Sending A Signal From Main Function To A Thread?
I am using Pyqt4 to make GUI in python. I want to send a signal from the main GUI to a thread in order to make a function. I know how to send a signal from thread to the main GUI b
Solution 1:
The proper way is to create object which will be moved to thread. You don't need to subclass QtCore.QThread
!
More or less something like that (disclaimer: I'm more C++ than Python):
myThread = QtCore.QThread()
testObject = YourClassWithSlots() # NO PARENT HERE
testObject.moveToThread(myThread)
myThread.start()
From that point all connected slots of testObject
(with default connection type) will be run in thread myThread
.
Useful links:
- http://blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/
- http://doc.qt.io/qt-5/qt.html#ConnectionType-enum
- http://doc.qt.io/qt-5/qobject.html#moveToThread
More details to please @Ezee
classYourClassWithSlots(QtCore.QObject) :
@Slot(int)defhavyTaskMethod(self, value) :
# long runing havy task
time.sleep(value)
self.myThread = QtCore.QThread(self)
self.testObject = YourClassWithSlots() # NO PARENT HERE
self.testObject.moveToThread(myThread) # this makes slots to be run in specified thread
self.myThread.start()
self.connect(Qt.SIGNAL("Signal(int)"), self.testObject.havyTaskMethod)
Post a Comment for "Sending A Signal From Main Function To A Thread?"