Results 1 to 3 of 3

Thread: Calling a slot from another thread

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Calling a slot from another thread

    Hey everyone, it's friday and I'm being dense so I came here for some help.

    I have two threads, one is my interface to Skype (through their API), and one is the GUI thread. A lot of the Skype API functions are blocking, thus the separate thread is necessary.

    However there is a lot of communication necessary between the two threads, mostly the GUI sending commands to the Skype thread to do things (call, answer, send message, log in, etc).

    So far to send a command I just can just create a signal in the GUI class, like "doCall" which gets hooked up to the "call" slot in the Skype thread. Due to the magic of queued connections, this works just fine.

    However it seems a bit cumbersome to have to define a signal for every command I want to send to the Skype thread.

    Is there a way I can just call a slot, but queue it up so that it gets called in the right thread?

    Something like skypeThread->invokeSlot(startSkypeCall("someguy")) instead of defining, connecting, and emitting a signal for each function I want to call.

  2. #2
    Join Date
    May 2008
    Posts
    155
    Thanked 15 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Calling a slot from another thread

    QMetaObject::invokeMethod(skypeThread, "startSkypeCall", Qt::QueuedConnection, Q_ARG(QString, "someguy")) might work

  3. The following 2 users say thank you to ktk for this useful post:

    pherthyl (19th March 2010), sadjoker (21st March 2010)

  4. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling a slot from another thread

    Thanks! it works.

    For completeness, in pyQt it works like this:
    QMetaObject.invokeMethod(skypeThread, "startSkypeCall", Qt.QueuedConnection, QtCore.Q_ARG("QString", "someguy"))

    But you have to be sure to mark your slot as a pyQtSlot. Otherwise it won't work, with the very helpful error: RuntimeError: QMetaObject.invokeMethod() call failed

  5. The following user says thank you to pherthyl for this useful post:

    wladimir (18th October 2011)

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 20:31
  2. Differences in calling a Slot
    By Denarius in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2009, 15:32
  3. Calling a slot with arguments
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 1st May 2008, 21:22
  4. Calling one new thread with in existing thread?
    By ashukla in forum Qt Programming
    Replies: 1
    Last Post: 24th September 2007, 14:09
  5. calling Gui treeWidget in Thread class
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2007, 09:14

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.