Results 1 to 4 of 4

Thread: Posting custom events to a subclass of QThread

  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Posting custom events to a subclass of QThread

    Is it not possible to post custom events (across thread) to a QThread descendant? Or am I doing something wrong? Should I create a child for the thread and post events to the child object to get events delivered to the correct thread?

    The code below illustrates the problem:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtDebug>
    3.  
    4. class MyThread : public QThread
    5. {
    6. public:
    7. MyThread(QObject* parent = 0) : QThread(parent)
    8. {
    9. }
    10.  
    11. protected:
    12. void run()
    13. {
    14. qDebug() << "MyThread::run()" << QThread::currentThread() << this->thread();
    15. exec();
    16. }
    17.  
    18. void customEvent(QEvent* event)
    19. {
    20. qDebug() << "MyThread::customEvent()" << QThread::currentThread() << this->thread();
    21. // a long loop here will naturally block GUI
    22. qDebug() << "LOOP BEGIN";
    23. for (uint i = 0; i < -1; ++i);
    24. qDebug() << "LOOP END";
    25. }
    26. };
    27.  
    28. class EventButton : public QPushButton
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. EventButton() : QPushButton("Post an event")
    34. {
    35. mythread = new MyThread(this); // creating without parent has no effect
    36. mythread->start();
    37. connect(this, SIGNAL(clicked()), this, SLOT(postEvent()));
    38. }
    39.  
    40. private slots:
    41. void postEvent()
    42. {
    43. qDebug() << "EventButton::postEvent()" << QThread::currentThread() << this->thread();
    44. QApplication::postEvent(mythread, new QEvent(QEvent::User));
    45. }
    46.  
    47. private:
    48. MyThread* mythread;
    49. };
    50.  
    51. int main(int argc, char *argv[])
    52. {
    53. QApplication a(argc, argv);
    54. qDebug() << "main()" << QThread::currentThread();
    55. EventButton b;
    56. b.show();
    57. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    58. return a.exec();
    59. }
    60.  
    61. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Please, spot a mistake there..
    J-P Nurmi

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Posting custom events to a subclass of QThread

    Is it not possible to post custom events (across thread) to a QThread descendant?
    Sure it is.
    Do you get the debug message in your postEvent() slot?

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Posting custom events to a subclass of QThread

    Quote Originally Posted by high_flyer
    Sure it is.
    Do you get the debug message in your postEvent() slot?
    I don't mean that the event wouldn't get delivered at all. Sure it does. But it will get sent directly and processed in the main thread, meaning the it will steal main thread's execution time and so the GUI will get blocked. The point with the debug message in MyThread::customEvent() is that it will show you the current executing thread, which is the main thread.

    I somewhy would have expected that posting a custom event to a QThread object would have got processed in it's own event loop. But it will get processed in it's owner's event loop instead (the main GUI thread in this case).
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Posting custom events to a subclass of QThread

    I somewhy would have expected that posting a custom event to a QThread object would have got processed in it's own event loop.
    I am not sure 100%, but I think this was the case in Qt3.
    At least thats is the way I worked with threads in Qt3 and it did work for me.
    But you are working now with Qt4.
    And here the docs say:
    A QObject instance is said to live in the thread in which it is created. Events to that object are dispatched by that thread's event loop.
    And since your button was created in the main loop, and the QThread in it is its child, the events get posted in the main event loop.

  5. The following user says thank you to high_flyer for this useful post:

    jpn (4th July 2006)

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.