Results 1 to 2 of 2

Thread: post event to QThread(Eventlool) from itself

  1. #1
    Join Date
    Nov 2009
    US, Midwest
    Thanked 4 Times in 4 Posts
    Qt products
    Unix/X11 Windows

    Default post event to QThread(Eventlool) from itself

    I have QThread-derived class (downloader) that has to download several files from the server sequentially. I post an event to the thread instance from main to start download, and then I am trying to post event to the thread (now a QEventLoop) for the next download. My question is: how to post the event from "within" the thread?

    Qt Code:
    1. class T : public QThread
    2. {
    3. public:
    4. void run() {
    5. exec();
    6. }
    7. bool event (QEvent* event) {
    8. //process download
    9. ...
    11. //code breaks here. Question - how to post an event?
    12. QApplication::instance()->postEvent(this, new userEvent());
    13. }
    14. }
    16. int main()
    17. {
    18. T t;
    19. t.start();
    20. QApplication::instance()->postEvent(&t, new userEvent());
    21. ...
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits

    Default Re: post event to QThread(Eventlool) from itself

    What you have will not work because you are posting an event to QThread object which does not live in the thread it represents. Instead your thread should have some QObject which you can post events to.


    Qt Code:
    1. QThread *thread = new QThread;
    2. thread->start(); // implies exec()
    3. DownloadHandler *handler = new DownloadHandler; // your object
    4. handler->moveToThread(thread);
    5. QApplication::postEvent(handler, new SomeEvent);
    To copy to clipboard, switch view to plain text mode 

    You can also use signals and slots, if you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.

  3. The following user says thank you to wysota for this useful post:

    TorAn (20th September 2016)

Similar Threads

  1. QThread and event loop
    By Crashz83 in forum Qt Programming
    Replies: 4
    Last Post: 18th August 2015, 09:38
  2. How to immediately post an event/signal to another thread
    By mortoray in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2010, 14:33
  3. QWidget post event
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2008, 19:07
  4. Request for comment: Post event filter
    By wysota in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 22:55
  5. How to post a key event to another application
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 14:09

Tags for this Thread


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.