Is QThread doc correct?
-
Hi,
In QThread docs, in detailed description section I see an example:
class Worker : public QObject { Q_OBJECT public slots: void doWork(const QString ¶meter) { QString result; /* ... here is the expensive or blocking operation ... */ emit resultReady(result); } signals: void resultReady(const QString &result); }; class Controller : public QObject { Q_OBJECT QThread workerThread; public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults); workerThread.start(); } ~Controller() { workerThread.quit(); workerThread.wait(); } public slots: void handleResults(const QString &); signals: void operate(const QString &); };
And it is correct?
Qt::QueuedConnection - The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
I think we should add
Qt::QueuedConnection
to:connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults);
I would like to execute doWork() method in other thread, so for me the correct is:
connect(this, &Controller::operate, worker, &Worker::doWork, Qt::QueuedConnection); connect(worker, &Worker::resultReady, this, &Controller::handleResults, Qt::QueuedConnection);
-
Hi,
In QThread docs, in detailed description section I see an example:
class Worker : public QObject { Q_OBJECT public slots: void doWork(const QString ¶meter) { QString result; /* ... here is the expensive or blocking operation ... */ emit resultReady(result); } signals: void resultReady(const QString &result); }; class Controller : public QObject { Q_OBJECT QThread workerThread; public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults); workerThread.start(); } ~Controller() { workerThread.quit(); workerThread.wait(); } public slots: void handleResults(const QString &); signals: void operate(const QString &); };
And it is correct?
Qt::QueuedConnection - The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
I think we should add
Qt::QueuedConnection
to:connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults);
I would like to execute doWork() method in other thread, so for me the correct is:
connect(this, &Controller::operate, worker, &Worker::doWork, Qt::QueuedConnection); connect(worker, &Worker::resultReady, this, &Controller::handleResults, Qt::QueuedConnection);
@qwe3 said in Is QThread doc correct?:
And it is correct?
it is.
The default parameter for QObject::Connect() is Qt::AutoConnection, which will be automatically Qt::QeueudConnection if its across threads.
You very rarely need to force a connection type
-
@qwe3 said in Is QThread doc correct?:
I thought that default is DirectConnection
Take a look at the connect() signature: https://doc.qt.io/qt-5/qobject.html#connect-2