Hi,

Does anyone know what has happened to QApplication:: postEvent and multithreading in Qt 4? If I post an event in another thread than the gui thread, the event is never handled.

Consider this minimal program:
Qt Code:
  1. #include <Qt/qapplication.h>
  2. #include <Qt/qthread.h>
  3. #include <iostream>
  4.  
  5. struct Terminator : QObject {
  6. bool event(QEvent* event) {
  7. std::cerr << "Terminator::event" << std::endl;
  8. return true;
  9. }
  10. };
  11.  
  12. int main(int argc, char** argv) {
  13. std::cerr << "main thread: " << QThread::currentThreadId() << std::endl;
  14.  
  15. QApplication app(argc, argv);
  16. QApplication::postEvent(new Terminator,
  17. new QEvent(QEvent::User));
  18. return app.exec();
  19. }
To copy to clipboard, switch view to plain text mode 
A custom event that terminates the application is posted in the main loop. When QApplication::exec is called, the event is processed. Everything works as expected and the output is:

main thread: 140735005136720
Terminator::event

But if the event is posted in a different thread, it will not be handled. Here's a minimal thread class and accordingly adjusted main function:
Qt Code:
  1. struct MyThread : QThread {
  2. void run() {
  3. std::cerr << "MyThread: " << currentThreadId() << std::endl;
  4.  
  5. QApplication::postEvent(new Terminator,
  6. new QEvent(QEvent::User));
  7. }
  8. };
  9.  
  10. int main(int argc, char** argv) {
  11. std::cerr << "main thread: " << QThread::currentThreadId() << std::endl;
  12.  
  13. QApplication app(argc, argv);
  14.  
  15. MyThread mt;
  16. mt.start();
  17.  
  18. return app.exec();
  19. }
To copy to clipboard, switch view to plain text mode 
The output of the program is:

main thread: 139728441435984
MyThread: 1099274576

which means that Terminator::event is never called.

The system is a standard Fedora (2.6.27.5-41.fc9.x86_64) with pthreads. Everything worked in Qt 3, so somewhere along the way something has changed. Any clues?