Before this question is closed as duplicate: The question that was asked about this same problem is from 2009 and Qt changed how threading should be implemented.
My Qt application has a main thread and a worker thread. I simplified the code for the question, but the problem is as follows:
If The MainWindow object emits a signal, that must be received by the slot of the worker object in a different QThread.
The main functions looks as follows:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w(0, [lot of irrelevant argumtents], ...)
QThread* thread = new QThread;
Worker *worker = new Worker(&w);
QObject::connect(&w, SIGNAL(demoButtonClicked()), worker, SLOT(DemoButton()));
QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
worker->moveToThread(thread);
thread->start();
w.show();
a.exec();
}
The (simplified) worker class looks as follows:
class Worker : public QObject
{
Q_OBJECT
public:
Worker(MainWindow *w);
public slots:
void process();
void DemoButton();
};
void Worker::DemoButton(){
cout << "Slot for demo button executed from worker thread!" << endl;
}
And the mainwindow class contains this function and just the signal defined in the header:
void MainWindow::on_pushButton_3_clicked()
{
std::cout << "Signal for demo button emitted!" << std::endl;
emit demoButtonClicked();
}
When I exectute the program, the on_pushButton_3_clicked function is executed, but not the slot in the worker object.
发现问题, Worker::process
确实阻塞了事件循环。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.