57 lines
1.5 KiB
C++
57 lines
1.5 KiB
C++
#include <QApplication>
|
|
#include <QFileDialog>
|
|
#include <QSemaphore>
|
|
#include <QThread>
|
|
#include <QTextStream> // Still needed for parsing the content
|
|
#include <QDebug>
|
|
#include <iostream>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
QApplication app(argc, argv);
|
|
|
|
QSemaphore sem(1);
|
|
sem.acquire();
|
|
|
|
int returnCode = -1;
|
|
|
|
auto fileContentReady = [&returnCode, &sem](const QString &fileName, const QByteArray &fileContent) {
|
|
if (fileName.isEmpty() || !QFile(fileName).exists()) {
|
|
std::cerr << "Failed to open file" << std::endl;
|
|
returnCode = 1;
|
|
sem.release();
|
|
return;
|
|
}
|
|
|
|
QString contentStr(fileContent);
|
|
QTextStream in(&contentStr);
|
|
QString line = in.readLine();
|
|
|
|
bool ok;
|
|
int number = line.toInt(&ok);
|
|
if (!ok) {
|
|
std::cerr << "Failed to read integer from file content" << std::endl;
|
|
returnCode = 1;
|
|
sem.release();
|
|
return;
|
|
}
|
|
|
|
std::cout << number * 2 << std::endl;
|
|
returnCode = 0;
|
|
sem.release();
|
|
};
|
|
|
|
QFileDialog::getOpenFileContent("All Files (*.*)", fileContentReady);
|
|
|
|
QThread *thread = QThread::create([&sem, &returnCode] () {
|
|
sem.acquire();
|
|
if (returnCode == -1) {
|
|
std::cerr << "Failed to open file dialog" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
return returnCode;
|
|
});
|
|
thread->start();
|
|
|
|
// TODO: currently does not work
|
|
} |