Results 1 to 6 of 6

Thread: Handler thread to process messages asynchronously

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handler thread to process messages asynchronously

    This is somehow what you were talking about? Or am I completely off the way?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QEventLoop>
    3. #include <QWaitCondition>
    4. #include <QMutex>
    5. #include <QThread>
    6.  
    7. class MyEventLoop : public QThread
    8. {
    9. public:
    10. MyEventLoop() {}
    11. bool event(QEvent* event)
    12. {
    13. qDebug("Processing event.");
    14. return true;
    15. }
    16. void run() {exec();}
    17. };
    18.  
    19. int main(int argc, char* argv[])
    20. {
    21. QCoreApplication a(argc, argv);
    22. QThread thread;
    23. thread.start();
    24.  
    25. for (int i = 0; i < 10; i++) {
    26. QMutex mutex;
    27. cond.wait(&mutex, qrand() % 4000);
    28.  
    29. qDebug("Sending event.");
    30. QCoreApplication::postEvent(&thread, new QEvent(QEvent::Hide));
    31. }
    32.  
    33. return a.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    I would like to use this even following the command design pattern, so sending requests in the form of objects to better describe the request. Sill this is a good way to go? I suppose I have to subclass QEvent to add whatever else I need right?

    Speaking about memory management, the documentation obviously tell me to allocate the object in the heap, but does it free memory automatically after the event(...) method has returned true or am I supposed to do it there myself?

    The approach I reported in the other post was correct as well?

    Thanks!
    Last edited by Luc4; 24th April 2011 at 12:22.

Similar Threads

  1. creating widgets asynchronously
    By tampstaffs in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2009, 10:30
  2. How to communicate Qt Process with non-qt process
    By nrabara in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 15th February 2009, 21:01
  3. Asynchronously playing a sound (.wav) on windows mobile
    By TMan in forum Qt for Embedded and Mobile
    Replies: 10
    Last Post: 20th October 2008, 19:16
  4. GUI Thread getting no time to process
    By steg90 in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2007, 09:29
  5. Qt4 no debug messages
    By TheKedge in forum Newbie
    Replies: 3
    Last Post: 23rd January 2006, 17:52

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
  •  
Qt is a trademark of The Qt Company.