Results 1 to 12 of 12

Thread: Communication between Threads

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2006
    Location
    Dortmund, Germany
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Communication between Threads

    hello.

    my little app has two threads:

    Thread 1: the regular main thread, where GUI rendering and short term computation is done.
    Thread 2: a worker thread for time consuming actions (file copy session).

    i want the worker thread to notify a custom made progress dialog, consisting of two QProgressBars and a QTextEdit, when to display new infos. can this be done by posting custom QEvents? i'm not sure because the signal / slot mechanism also doesn't work between threads.

    wan-hi
    Last edited by Wan-Hi; 19th March 2006 at 04:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    Quote Originally Posted by Wan-Hi
    I'm not sure because because the signal / slot mechanism also doesn't work between threads.
    It does work in Qt4, but you must use queued connections.

  3. #3
    Join Date
    Mar 2006
    Location
    Dortmund, Germany
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    i did use queued connections, trying to notify the main thread by the worker thread. but i get errors, although everything seems to be right.

    Qt Code:
    1. CustomProgressDialog::CustomProgressDialog(Task * task, QWidget * parent) : QDialog(parent) {
    2. ...
    3. connect(task, SIGNAL(stepCompleted(QString)), this, SLOT(stepCompleted(QString)), Qt::QueuedConnection);
    4. ...
    5. }
    6.  
    7. void CustomProgressDialog::stepCompleted(QString msg) {
    8. QTextCursor cursor(doc);
    9. cursor.movePosition(QTextCursor::End);
    10. taskEdit->setTextCursor(cursor);
    11. taskEdit->insertPlainText(description);
    12. }
    To copy to clipboard, switch view to plain text mode 

    during debugging i always get an assert error, saying that new widgets may only be created in the UI thread (main thread). this happens as soon as the slot function is executed. it's irritating because per definition all slots of QueuedConnections are executed in the thread of the target's thread, which in this case IS the UI thread.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    How do you create that CustomProgressDialog instance? What is that Task class? Does it inherit QThread?

  5. #5
    Join Date
    Mar 2006
    Location
    Dortmund, Germany
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    yes, Task inherits QThread. the CustomProgressDialog is created on the stack within the main UI thread, inside a MainWindow's function to be more exact. strange...

  6. #6
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Communication between Threads

    To post data from thread to gui i used custom QEvent... here is example:

    Qt Code:
    1. class printE: public QEvent
    2. {
    3. public:
    4. printE(QString str, Type type=(QEvent::Type)1001); // here should be int between 1000 and 65535
    5. Type type();
    6. QString data();
    7.  
    8. private:
    9. Type t;
    10. QString string;
    11. };
    12.  
    13. printE::printE(QString str, Type type):QEvent(type)
    14. {
    15. t = type;
    16. string = str;
    17. }
    18.  
    19. QEvent::Type printE::type()
    20. {
    21. return t;
    22. }
    23.  
    24. QString printE::data()
    25. {
    26. return string;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Of course you can add much more data into this event... In your customEvent(QEvent *e) you should place something like that to recieve data from your custom event

    Qt Code:
    1. if(e->type() == (QEvent::Type)1001)
    2. {
    3. QString str=((printE*)e)->data();
    4. // do something with str
    5. }
    To copy to clipboard, switch view to plain text mode 

    I hope it'll help somehow

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    Quote Originally Posted by Wan-Hi
    yes, Task inherits QThread. the CustomProgressDialog is created on the stack within the main UI thread, inside a MainWindow's function to be more exact. strange...
    Indeed, something strange is going on.

  8. #8
    Join Date
    Mar 2006
    Location
    Dortmund, Germany
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    thanks for the help. i was thinking about using custom QEvents, but i managed to make queued connections work. it turned out that a QMessageBox::information got left over in the worker thread which i forgot to remove.

  9. #9
    Join Date
    Jan 2013
    Posts
    1
    Qt products
    Platforms
    Symbian S60

    Default Re: Communication between Threads

    I was also felt help in code.. So now it is resolved. It is good to see this info about to communication in threads..

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    Quote Originally Posted by Wan-Hi View Post
    thanks for the help. i was thinking about using custom QEvents, but i managed to make queued connections work.
    You only need custom events when doing that kind of communication in Qt3 which didn't have Qt::QueuedConnection yet.

    Cheers,
    _

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Communication between Threads

    Kevin, it's a seven year old zombie thread
    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.


  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Communication between Threads

    Oh boy, didn't check

    Anyway, my reply's content still stands

    Cheers,
    _

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49
  4. Threads communication
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 31st March 2006, 14:46

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.