1

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 but I don't know the reverse thing. Also I want to pass an variable with signal.

something like:

class MainGUI()
     def function
         value = 5
         self.emit(QtCore.Signal("Signal(int)"),value)


class thread(QtCore.QThread)
     def _init_(self)
          Qtcore.QThread._init_()
          self.connect(Qt.SIGNAL("Signal(int)"),self.run())

     def run(self,value):
         time.sleep(value)

So every time that the signal is transmitted from the main GUI I want to run the function in the thread in parallel without freezing the GUI.

Any help would be appreciated, Jet

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Jetmir
  • 21
  • 2

1 Answers1

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:


More details to please @Ezee

class YourClassWithSlots(QtCore.QObject) :
    @Slot(int)
    def havyTaskMethod(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)
honk
  • 9,137
  • 11
  • 75
  • 83
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Question is about sending a signal not about using a thread. So it's not an answer. In this case OP's next question would be "how to send a signal from an object moved to another thread" – Ezee Oct 10 '14 at 08:41
  • read links I've given before voting down. Apparently you like vengeance. Note that even after edits your answer is crappy. @Jetmir should understand functionality of signal and slots (and you too) then his problem will be trivial to solve. – Marek R Oct 10 '14 at 09:02
  • Links can lead to more information but not to the answer. Add something that answers the OP's question and the vote down will be removed. Think about being more polite also. – Ezee Oct 10 '14 at 09:06
  • here you have more details. – Marek R Oct 10 '14 at 09:19
  • You don't need to inherit `YourClassWithSlots` from `QThread`. – Ezee Oct 10 '14 at 09:20
  • copy paste mistake :). fixed. – Marek R Oct 10 '14 at 09:24
  • Ok. Also correct the last line where you are trying to connect to the slot, but didn't specified an object. – Ezee Oct 10 '14 at 09:27
  • I think it should be `self.testObject.connect` but let it check to python experts, we both are more c++ guys. Now your answer is mostly correct. At least the logic is proper. Voted up. – Ezee Oct 10 '14 at 09:53