We'd need to see the actual code. Bear in mind that QThread object lives in the main thread and not in the thread it represents, that's probably your issue.
Anyway, I suggest you use QtConcurrent::run() instead of a standalone QThread. For such simple usecases it's much easier to use.
return stream.readAll();
}
// ...
QFutureWatcher<QString> *futureWatcher = new ...; // optional
connect(futureWatcher, SIGNAL(finished()), this, SLOT(...))); // optional
QFuture<QString> future = QtConcurrent::run(loadTextFile, QStringLiteral("somefile.txt"));
futureWatcher->setFuture(future); // optional
// ... do something in the meantime
QString content
= future.
result();
// will block if the result is not ready (alternative to useing QFutureWatcher)
QString loadTextFile(const QString &fileName) {
QFile f(fileName);
if(!f.open(...)) return QString();
QTextStream stream(&f);
return stream.readAll();
}
// ...
QFutureWatcher<QString> *futureWatcher = new ...; // optional
connect(futureWatcher, SIGNAL(finished()), this, SLOT(...))); // optional
QFuture<QString> future = QtConcurrent::run(loadTextFile, QStringLiteral("somefile.txt"));
futureWatcher->setFuture(future); // optional
// ... do something in the meantime
QString content = future.result(); // will block if the result is not ready (alternative to useing QFutureWatcher)
To copy to clipboard, switch view to plain text mode
Bookmarks