Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect PyQt slot from background thread to gui thread

Tags:

python

pyqt

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).

like image 582
Ian Avatar asked Jan 16 '11 22:01

Ian


People also ask

Is PyQt5 Multithreaded?

PyQt provides a complete, fully integrated, high-level API for doing multithreading.

What is QThread Python?

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() .

What is pyqtSignal?

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.

Is PyQt thread safe?

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).


1 Answers

you forgot this:

def __init__(self,model,view):
    super(CompanyPresenter, self).__init__() # this!!!!!!!!!

add this will work.(tested)

like image 186
linjunhalida Avatar answered Oct 21 '22 05:10

linjunhalida