Results 1 to 10 of 10

Thread: signals/slots across threads

Hybrid View

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

    Default Re: signals/slots across threads

    I am not interested in DirectConnection - that is the whole idea of having the thread for.
    I want to post the signals, and let the target thread execute them in its own time.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: signals/slots across threads

    Hi Dani, nice to hear from you again I hope we can drink a 3EUR worth small bottle of water some time soon

    About the problem - as Jacek pointed out the problem of thread affinity causes Qt to misinterpret your intentions. HSCSocket (thread) is created with the GUI thread affinity and you then make a connection to the thread object that is handled by the GUI thread.
    There are two solutions:
    1. make the connection not to the thread object, but to an object created by the thread's run method
    or
    2. push the thread object to the thread itself, like so:
    Qt Code:
    1. connect(m_hpscSocket, SIGNAL(started()), this, SLOT(onStarted()));
    2. m_hpscSocket->start();
    3. //...
    4. void FrmPower::onStarted(){
    5. m_hpscSocket->moveToThread(m_hpscSocket);
    6. }
    To copy to clipboard, switch view to plain text mode 
    That just might work

    Remember you'll lose the parent-child relation between FrmPower object and the thread object.

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

    babu198649 (12th October 2009)

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

    Default Re: signals/slots across threads

    Hi Wysota and Jacek,

    Yes, it will be cool to have a 3EU drink again, I wont mind if it will be a trolltech event though
    Ok, I think I understand now.
    Hmm, I new I need to implement a queue, and I thought that if I use the thread safe signal/slots I will just let the QThread queue do the work, and all I need to do is post signals from my gui thread.
    That was what I was trying to do anyhow...
    But you are right, my SocketThread class it self lives in the GUI thread.
    I guess I will just fall back to custom events then.
    I guess that the threaded signals/slots are doing just that behind the scenes, but for me it will be less coding with custom events.

    Thanks again for the first class help guys!

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

    Default Re: signals/slots across threads

    If you want a queue, maybe it's better to use postEvent() directly? Signals/slots won't give you any advantage here...

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


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

    Default Re: signals/slots across threads

    Yes, its exactly what I meant when I said custom events.
    Thanks.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: signals/slots across threads

    Yes, I know I just noticed that after sending my post and added the second sentence just to make the post say something anyway

Similar Threads

  1. Querying within Threads
    By bera82 in forum Qt Programming
    Replies: 11
    Last Post: 12th March 2008, 01:08
  2. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  3. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. [QT4] threads, signals, and slots, please help.
    By ucntcme in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2006, 14:23

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.