Results 1 to 3 of 3

Thread: Emitting signal from a non-Qt Thread

  1. #1
    Join Date
    Jun 2013
    Posts
    20
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Emitting signal from a non-Qt Thread

    Hello people!

    What i want to do essentially is send a signal from a non-Qt Thread to a Slot in another Thread.

    What I have now is something like this (bit of pseudocode so bear with me):

    Qt Code:
    1. class CallBackClass : QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. CallBackClass(){ // fancy stuff
    6.  
    7. notWorkingSignalConnectionClass = new emittingClass();
    8. connect(notWorkingSignalConnectionClass, SIGNAL(dataReady()), this, SLOT(takeData()), Qt::QueuedConnection);//also tried auto connection. same result
    9. }
    10.  
    11. static int callback(char* field, int len)
    12. {
    13. //other things
    14.  
    15. notWorkingSignalConnectionClass->setData(field, len);
    16.  
    17. }
    18.  
    19. public slots:
    20. void takeData()
    21. {
    22. //i want magic to happen here, but this function is never called
    23. }
    24.  
    25. private:
    26.  
    27. static emittingClass* notWorkingSignalConnectionClass;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class emittingClass : QObject
    2. {
    3. Q_OBJECT
    4. signals:
    5. void dataReady();
    6.  
    7. public:
    8. void setData(char* field, int len)
    9. {
    10. //do fancy stuff with field
    11. emit dataReady();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. emittingClass* CallBackClass::notWorkingSignalConnectionClass = new emittingClass();
    2.  
    3. int main
    4. {
    5. CallBackClass callbackInstance();
    6. //I am starting things here, in particular there is a thread running for handling external data input / output
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 


    Now what essentially is going on is I have an external library that is handling input / output on a USB device. This library uses its own threads and relies on static callback functions in order to tell me that data is ready. that is the callback() function in the CallBackClass. This function is always called in one particular thread that is different from the main thread in which I actually construct all other objects above.

    All the signal slot connections are working fine as long as I am emitting the signal in the same thread, but it is not working when I try to do it from a different thread.

    Is there a way to make that work?
    Last edited by FreddyKay; 29th January 2016 at 10:01.

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

    Default Re: Emitting signal from a non-Qt Thread

    I think in this case it is easier to just call the slot via QMetaObject:.invokeMethod(), i.e. no need for an intermediary "emitter" object.

    Qt Code:
    1. QMetaObject::invokeMethod(receiver, "takeData", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    You can even pass arguments that way.

    Cheers,
    _

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

    FreddyKay (29th January 2016)

  4. #3
    Join Date
    Jun 2013
    Posts
    20
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Emitting signal from a non-Qt Thread

    True, thanks.

    Anyway, the problem was at another point of the program entirely and prevented it from starting the even loop in the first place, which is why my signal / slots would not connect. Solved now.

Similar Threads

  1. Replies: 10
    Last Post: 3rd January 2015, 12:25
  2. Issue emitting signal from background thread.
    By Chris@Link in forum Newbie
    Replies: 8
    Last Post: 3rd July 2012, 17:56
  3. Emitting signals in other thread
    By mvbhavsar in forum Newbie
    Replies: 9
    Last Post: 8th June 2011, 08:17
  4. Replies: 5
    Last Post: 22nd February 2011, 21:21
  5. Emitting signal from DLL to EXE
    By Miihkali in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2009, 08:32

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.