简体   繁体   English

具有多个线程的Qt应用程序

[英]Qt application with multiple threads

I have a QT application. 我有一个QT申请。

I have a thread (ex thread1 ) that shows my mainwindow (so this is different from the main thread). 我有一个显示我的主窗口的线程(ex thread1 )(因此这与主线程不同)。

I also have my own thread (ex thread2 ) must notify the thread1 updating my GUI. 我也有我自己的线程(ex thread2 ),必须通知线程1更新我的GUI。 There is a quick way to do this? 有一个快速的方法吗? ex: thread2 may decide to update an image: so, he shall notify the thread1 to perform the method changeImage() . 例如:thread2可能决定更新映像:因此,他将通知thread1执行方法changeImage()

If I do run this method directly from thread2 my application will have a crash. 如果我确实直接从thread2运行此方法,则我的应用程序将崩溃。 I must to use pthread. 我必须使用pthread。

Only the main thread should update the GUI (for restrictions due to graphical systems). 只有主线程才能更新GUI(由于图形系统的限制)。

However, Qt has enhanced thread support allowed by signal-slot connections across threads. 但是,Qt具有跨线程的信号槽连接所允许的增强的线程支持。 In facts, the other threads have to notify the main thread using signals ( emit my_signal() see http://qt-project.org/doc/qt-4.8/signalsandslots.html ) or using QCoreApplication::postEvent ( http://qt-project.org/doc/qt-4.8/qcoreapplication.html#postEvent-2 ) emit my_signal() ,其他线程必须使用信号来通知主线程( emit my_signal()参见http://qt-project.org/doc/qt-4.8/signalsandslots.html )或使用QCoreApplication::postEventhttp:/ /qt-project.org/doc/qt-4.8/qcoreapplication.html#postEvent-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM