[Qt-interest] Forcing another specific thread to execute slots
Mihail Naydenov
mlists at ymail.com
Sat Dec 12 10:12:33 CET 2009
>
>From: Preet <preet.desai at gmail.com>
>To: qt-interest at trolltech.com
>Sent: Sat, December 12, 2009 8:54:01 AM
>Subject: [Qt-interest] Forcing another specific thread to execute slots
>
>>Hey all,
>
>This is probably an easy question for some of you =)
>I want my main thread to signal slots in secondary threads under certain situations. Each of these secondary threads are the same subclass of QThread and have the same slots available. Based on user input, I only want to signal specific secondary threads. So for example the slots I have in each secondary thread are SlotA, SlotB and SlotC. To invoke these slots from the main thread I can create a different signal for each Thread and Slot (SignalThread1SlotA, SignalThread1SlotB, etc), but that seems like a huge waste since I could have a lot of threads and slots. I can also emit one signal from the main thread for all the secondary threads (SignalSlotA triggers SlotA in Thread1, Thread2, Thread3, etc) and use QObject::connect/disconnect to selectively signal threads. This also seems incorrect.
>I'm not invoking the slot like a normal function call because I don't want the calling thread to execute the slot. Is there another way to do this? Can I create signals within the secondary threads (that call these slots) and use 'emit otherThread->SignalBlah()'? ie. will that still get otherThread to execute the respective slot?
It seams you need QMetaObject::invokeMethod(...). This will execute the specified slot of the selected object in the "correct" thread.
The syntax is pretty close to your "emit otherThread->SignalBlah()"
QMetaObject::invokeMethod(otherThread, "SignalBlah");
MihailNaydenov
PS As usual, don't forget that slots are executed in the thread the QThread object itself lives in, not the one it creates for its run().
So, in the scenario above, 'SignalBlah' will be executed in a thread, diff. than the calling, only if 'otherThread' object lives in (it is moved to) another thread.
>
>
>Regards,
>
>Preet
>
More information about the Qt-interest-old
mailing list