I wish to connect up a signal in the background thread to a slot in the GUI thread in a pythonic way.
I have the following code snippet.
from PyQt4.QtCore import *
class CompanyPresenter(QObject):
fieldChangeSignal = pyqtSignal(str, str)
def __init__(self,model,view):
self.model = model # a CompanyModel
self.view = view # a CompanyView
self.fieldChangeSignal.connect(view.setField)
I get this error (on the connect line)
TypeError: pyqtSignal must be bound to a QObject, not 'CompanyPresenter'
But CompanyPresenter inherits from QObject so it is a QObject. What is happening?
(I want the Presenter and GUI to run in different threads eventually, but I have not got that far yet. There is no threading yet).
PyQt provides a complete, fully integrated, high-level API for doing multithreading.
A QThread object manages one thread of control within the program. QThreads begin executing in run() . By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread. You can use worker objects by moving them to the thread using moveToThread() .
pyqtSignal (types[, name[, revision=0[, arguments=[]]]]) Create one or more overloaded unbound signals as a class attribute. Parameters: types – the types that define the C++ signature of the signal. Each type may be a Python type object or a string that is the name of a C++ type.
While some parts of the Qt framework are thread safe, much of it is not. The Qt C++ documentation provides a good overview of which classes are reentrant (can be used to instantiate objects in multiple threads).
you forgot this:
def __init__(self,model,view):
super(CompanyPresenter, self).__init__() # this!!!!!!!!!
add this will work.(tested)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With