Results 1 to 6 of 6

Thread: Invoking a slot in another thread

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Invoking a slot in another thread

    Hello,

    I am trying to use invokeMethod from the main thread on the slot of a QThread subclass which is running an event loop.

    Qt Code:
    1. QMetaObject::invokeMethod(myThread, "mySlot", Qt::QueuedConnection, Q_ARG(const SomeObject&, obj));
    To copy to clipboard, switch view to plain text mode 

    However, outputting QThread::currentThreadId() inside the slot shows that it executes in the main thread. Does anyone know what the problem is? I would prefer not to connect a signal to the slot.

    Thanks!

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking a slot in another thread

    You'll probably need to post some of your threading code, but have you tried moveToThread?

  3. #3
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Invoking a slot in another thread

    You'll probably need to post some of your threading code, but have you tried moveToThread?
    How would moveToThread help in this case? Do you mean move the QThread object into its own thread?

  4. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Invoking a slot in another thread

    Which thread owns the QThread? Likely you created it in the main thread and it's still owned by the main thread, so that's where any slots will be executed.

  5. #5
    Join Date
    Aug 2009
    Posts
    52
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Invoking a slot in another thread


  6. #6
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Invoking a slot in another thread

    Ah, I understand now. My QThread object lives in the main thread, but I thought signals and slots for the QThread would execute inside it's thread context. Thanks for the link, dbzhang800.

Similar Threads

  1. QThread - Using a slot to exit the thread
    By johnnyturbo3 in forum Qt Programming
    Replies: 7
    Last Post: 27th April 2011, 12:36
  2. Quit thread using signal/slot
    By mounte in forum Qt Programming
    Replies: 5
    Last Post: 31st January 2011, 21:14
  3. QThread slot executed in GUI thread
    By tnyblom in forum Qt Programming
    Replies: 13
    Last Post: 25th May 2010, 08:49
  4. Calling a slot from another thread
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2010, 23:13
  5. Replies: 9
    Last Post: 28th November 2009, 21:31

Tags for this Thread

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.