It's maybe a stupid question but why I can not emit a signal self.printSecondSignal.emit('TEST2') in the Worker class. What I want to do is: Calling the function do_some_calc() (connecteded over QPushButton clicked() signal) from TestMain class and in Worker class, want to emit a signal to update the Gui in TestMain. But I get AttributeError: TestMain object has no attribute printSecondSignal. Emitting the signal self.printFirstSignal.emit('TEST1') works without any problem
TestMain.py
from source.Manager.TestManager import *
class TestMain(QMainWindow):
def __init__(self, parent= None):
super(TestMain, self).__init__(parent)
# import ui
loadUi('../gui/testGui.ui', self)
self.manager= TestManager()
self.thread= QThread()
self.manager.moveToThread(self.thread)
self.manager.finished.connect(self.thread.quit)
self.thread.started.connect(self.manager.first_slot)
self.thread.start()
self.manager.printFirstSignal.connect(self.print_onTextEdit)
self.offlinePushButton.clicked.connect(self.offline_slot)
self.manager.printSecondSignal.connect(self.print_onTextEdit)
def offline_slot(self):
manager.do_some_calc(self)
def print_onTextEdit(self, str):
self.outputTextEdit.append(str)
Manager.py
class TestManager(QObject): #QtCore.QThread
finished= pyqtSignal()
printFirstSignal= pyqtSignal(str)
printSecondSignal= pyqtSignal(str)
def __init__(self, parent= None):
super(TestManager, self).__init__(parent)
def first_slot(self):
self.printFirstSignal.emit('TEST1')
self.finished.emit()
def do_some_calc(self):
do_sometingelse()
try:
self.printSecondSignal.emit('TEST2')
except :
traceback.print_exc()
As you were told in the other question you asked about this same code, all communication between threads has to be done through use of a signal. In your offline_slot method you attempt to call the manager.do_some_calc() directly while the manager instance is operating in another thread. This communication will have to be done with a signal. It would look something like this:
class TestMain(QMainWindow):
do_some_calc_signal = pyqtSignal()
def __init__(self, parent=None):
super(TestMain, self).__init__(parent)
# import ui
loadUi('../gui/testGui.ui', self)
self.manager= TestManager()
self.thread= QThread()
self.manager.moveToThread(self.thread)
self.do_some_calc_signal.connect(self.manager.do_some_calc)
self.manager.finished.connect(self.thread.quit)
self.thread.started.connect(self.manager.first_slot)
self.thread.start()
self.manager.printFirstSignal.connect(self.print_onTextEdit)
self.offlinePushButton.clicked.connect(self.offline_slot)
self.manager.printSecondSignal.connect(self.print_onTextEdit)
def offline_slot(self):
self.do_some_calc_signal.emit()
Related
If I define a disable_signal = QtCore.pyqtSignal() outside of the custom button class, the behaviour I am looking for (all instance are disabled when clicked upon).
class CustomButton(QtWidgets.QToolButton):
def __init__(self, parent, disable_signal):
super(CustomButton, self).__init__(parent)
self.disable_signal = disable_signal
self.disable_signal.connect(self.disable)
self.pressed.connect(self.buttonPressed)
def buttonPressed(self):
self.disable_signal.emit()
#QtCore.pyqtSlot()
def disable(self):
print("received")
self.setEnabled(False)
However, if I define the signal as a class attribute, each instance behave as if they each had their individual signal (pressing upon one, disable only that one):
class CustomButton(QtWidgets.QToolButton):
disable_signal = QtCore.pyqtSignal()
def __init__(self, parent):
super(CustomButton, self).__init__(parent)
self.disable_signal.connect(self.disable)
self.pressed.connect(self.buttonPressed)
def buttonPressed(self):
self.disable_signal.emit()
#QtCore.pyqtSlot()
def disable(self):
print("received")
self.setEnabled(False)
I don't understand why the signal is not shared? I tried to use instead of self.disable_signal.connect(self.disable), CustomButton.disable_signal.connect(self.disable) but I get the error: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'.
I have this code (if you have pyqt5, you should be able to run it yourself):
import sys
import time
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
class Worker(QObject):
def __init__(self):
super().__init__()
self.thread = None
class Tab(QObject):
def __init__(self, _main):
super().__init__()
self._main = _main
class WorkerOne(Worker):
finished = pyqtSignal()
def __init__(self):
super().__init__()
#pyqtSlot(str)
def print_name(self, name):
for _ in range(100):
print("Hello there, {0}!".format(name))
time.sleep(1)
self.finished.emit()
self.thread.quit()
class SomeTabController(Tab):
def __init__(self, _main):
super().__init__(_main)
self.threads = {}
self._main.button_start_thread.clicked.connect(self.start_thread)
# Workers
self.worker1 = WorkerOne()
#self.worker2 = WorkerTwo()
#self.worker3 = WorkerThree()
#self.worker4 = WorkerFour()
def _threaded_call(self, worker, fn, *args, signals=None, slots=None):
thread = QThread()
thread.setObjectName('thread_' + worker.__class__.__name__)
# store because garbage collection
self.threads[worker] = thread
# give worker thread so it can be quit()
worker.thread = thread
# objects stay on threads after thread.quit()
# need to move back to main thread to recycle the same Worker.
# Error is thrown about Worker having thread (0x0) if you don't do this
worker.moveToThread(QThread.currentThread())
# move to newly created thread
worker.moveToThread(thread)
# Can now apply cross-thread signals/slots
#worker.signals.connect(self.slots)
if signals:
for signal, slot in signals.items():
try:
signal.disconnect()
except TypeError: # Signal has no slots to disconnect
pass
signal.connect(slot)
#self.signals.connect(worker.slots)
if slots:
for slot, signal in slots.items():
try:
signal.disconnect()
except TypeError: # Signal has no slots to disconnect
pass
signal.connect(slot)
thread.started.connect(lambda: fn(*args)) # fn needs to be slot
thread.start()
#pyqtSlot()
def _receive_signal(self):
print("Signal received.")
#pyqtSlot(bool)
def start_thread(self):
name = "Bob"
signals = {self.worker1.finished: self._receive_signal}
self._threaded_call(self.worker1, self.worker1.print_name, name,
signals=signals)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Thread Example")
form_layout = QVBoxLayout()
self.setLayout(form_layout)
self.resize(400, 400)
self.button_start_thread = QPushButton()
self.button_start_thread.setText("Start thread.")
form_layout.addWidget(self.button_start_thread)
self.controller = SomeTabController(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
_main = MainWindow()
_main.show()
sys.exit(app.exec_())
However WorkerOne still blocks my GUI thread and the window is non-responsive when WorkerOne.print_name is running.
I have been researching a lot about QThreads recently and I am not sure why this isn't working based on the research I've done.
What gives?
The problem is caused by the connection with the lambda method since this lambda is not part of the Worker so it does not run on the new thread. The solution is to use functools.partial:
from functools import partial
...
thread.started.connect(partial(fn, *args))
Complete Code:
import sys
import time
from functools import partial
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
class Worker(QObject):
def __init__(self):
super().__init__()
self.thread = None
class Tab(QObject):
def __init__(self, _main):
super().__init__()
self._main = _main
class WorkerOne(Worker):
finished = pyqtSignal()
def __init__(self):
super().__init__()
#pyqtSlot(str)
def print_name(self, name):
for _ in range(100):
print("Hello there, {0}!".format(name))
time.sleep(1)
self.finished.emit()
self.thread.quit()
class SomeTabController(Tab):
def __init__(self, _main):
super().__init__(_main)
self.threads = {}
self._main.button_start_thread.clicked.connect(self.start_thread)
# Workers
self.worker1 = WorkerOne()
#self.worker2 = WorkerTwo()
#self.worker3 = WorkerThree()
#self.worker4 = WorkerFour()
def _threaded_call(self, worker, fn, *args, signals=None, slots=None):
thread = QThread()
thread.setObjectName('thread_' + worker.__class__.__name__)
# store because garbage collection
self.threads[worker] = thread
# give worker thread so it can be quit()
worker.thread = thread
# objects stay on threads after thread.quit()
# need to move back to main thread to recycle the same Worker.
# Error is thrown about Worker having thread (0x0) if you don't do this
worker.moveToThread(QThread.currentThread())
# move to newly created thread
worker.moveToThread(thread)
# Can now apply cross-thread signals/slots
#worker.signals.connect(self.slots)
if signals:
for signal, slot in signals.items():
try:
signal.disconnect()
except TypeError: # Signal has no slots to disconnect
pass
signal.connect(slot)
#self.signals.connect(worker.slots)
if slots:
for slot, signal in slots.items():
try:
signal.disconnect()
except TypeError: # Signal has no slots to disconnect
pass
signal.connect(slot)
thread.started.connect(partial(fn, *args)) # fn needs to be slot
thread.start()
#pyqtSlot()
def _receive_signal(self):
print("Signal received.")
#pyqtSlot(bool)
def start_thread(self):
name = "Bob"
signals = {self.worker1.finished: self._receive_signal}
self._threaded_call(self.worker1, self.worker1.print_name, name,
signals=signals)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Thread Example")
form_layout = QVBoxLayout()
self.setLayout(form_layout)
self.resize(400, 400)
self.button_start_thread = QPushButton()
self.button_start_thread.setText("Start thread.")
form_layout.addWidget(self.button_start_thread)
self.controller = SomeTabController(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
_main = MainWindow()
_main.show()
sys.exit(app.exec_())
To avoid blocking the gui for a slideshow function showing images i run the following
simple code.
A background Thread Class to wait one second, then signal to the gui waiting is finished.
class Waiter(QThread):
result = pyqtSignal(object)
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while self.isRunning:
self.sleep(1)
self.result.emit("waited for 1s")
Then in main window connect the slideshow start and stop button to start and stop methods of main app and connect the nextImage function to the signal the Waiter Thread emits.
self.actionstopSlideShow.triggered.connect(self.stopSlideShow)
self.actionslideShowStart.triggered.connect(self.startSlideShow)
self.waitthread = Waiter()
self.waitthread.result.connect(self.nextImage)
Then two methods of main app allow to start and stop the time ticker
def startSlideShow(self):
"""Start background thread that waits one second,
on wait result trigger next image
use thread otherwise gui freezes and stop button cannot be pressed
"""
self.waitthread.start()
def stopSlideShow(self):
self.waitthread.terminate()
self.waitthread.wait()
Up to now i have no problems subclassing from QThread in pyqt5, gui changes are all handled inside the main (gui) thread.
This question already has answers here:
PyQt: Connecting a signal to a slot to start a background operation
(3 answers)
Closed 5 years ago.
So having followed my best understanding of how to correctly use threads in Qt, I've written a small toy example in which a QObject is moved to a running thread and a signal is called on that object when the window is clicked.
The problem I'm having is the slot is never called in the correct thread - it is always called in the main thread.
This is implemented with the following code:
from PyQt5 import QtWidgets, QtCore
class WidgetWithInput(QtWidgets.QWidget):
widgetClicked = QtCore.pyqtSignal()
def mousePressEvent(self, event):
self.widgetClicked.emit()
class MyObject(QtCore.QObject):
aSignal = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super(MyObject, self).__init__(*args, **kwargs)
self.aSignal.connect(self.onASignal)
def onASignal(self):
print('MyObject signal thread: %s'
% str(int(QtCore.QThread.currentThreadId())))
class SimpleDisplay(QtWidgets.QMainWindow):
''' Class presenting the display subsytem
'''
def __init__(self, my_object, *args, **kwargs):
super(SimpleDisplay, self).__init__(*args, **kwargs)
self._my_object = my_object
self._widget = WidgetWithInput()
self.setCentralWidget(self._widget)
self._widget.widgetClicked.connect(self._do_something)
def _do_something(self):
self._my_object.aSignal.emit()
def main(run_until_time=None):
import sys
app = QtWidgets.QApplication(sys.argv)
print ('main thread: %s' % str(int(QtCore.QThread.currentThreadId())))
concurrent_object = MyObject()
display = SimpleDisplay(concurrent_object)
myobject_thread = QtCore.QThread()
myobject_thread.start()
concurrent_object.moveToThread(myobject_thread)
display.show()
exit_status = app.exec_()
myobject_thread.quit()
myobject_thread.wait()
sys.exit(exit_status)
if __name__ == '__main__':
main()
Which outputs something like:
main thread: 139939383113472
MyObject signal thread: 139939383113472
I would expect those two printed thread ids to be different.
Am I missing something here? Explicitly setting the signal to be queued doesn't change anything.
With thanks to #three_pinapples, his suggestion in the comments solves the problem: The slot needs to be decorated with QtCore.pyqtSlot:
class MyObject(QtCore.QObject):
aSignal = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super(MyObject, self).__init__(*args, **kwargs)
self.aSignal.connect(self.onASignal)
#QtCore.pyqtSlot()
def onASignal(self):
print('MyObject signal thread:\t %s'
% str(int(QtCore.QThread.currentThreadId())))
I am sending a signal from another class to update a PySide QTableWidget but nothing is coming though. I have made this very simple for this demonstration:
This is in the controller module called Records.py
class Records(QDialog, randomDialog.Ui_watchingDialog):
signal = 1
atSig = Signal(int)
def add_button_clicked(self):
# Do some stuff
self.signal = 1
self.atSig.emit(self.signal)
# Do some other Stuff
This sits out side the controller called main.py
from controller import Records
class main(QMainWindow, pyMainWindow.Ui_mainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
signal_records = Records.Records()
signal_records.atSig.connect(self.showNewData)
def showNewData(self, signal):
if signal == 1:
print "It worked!"
else:
print "Problem"
How come this signal is not coming through? No error messages are being thrown and neither of the print statements aren't being called. How can I fix this?
signal_records falls out of scope as soon as main.__init__() returns and is garbage collected. You need to make it a member of main so that it persists for the lifetime of the class.
self.signal_records = Records.Records()
self.signal_records.atSig.connect(self.showNewData)
Alternatively, you could assign main as the parent of signal_records
signal_records = Records.Records(self)
Both methods ensure a reference to signal_records sticks around.
If It's possibly, please define this before caller to receive;
.
.
#Slot (int)
def showNewData(self, signal):
.
.
Or it not, Please check your caller def add_button_clicked(self). I work in pyqt4 (same pySide) and (I cut some part out and put some path for test in your) code, It's work.
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class QRecordsDialog (QtGui.QDialog):
addButtonClickedSignal = QtCore.pyqtSignal(int)
def __init__ (self, parent = None):
super(QRecordsDialog, self).__init__(parent)
self.myQPushButton = QtGui.QPushButton('Test Signal', self)
self.myQHBoxLayout = QtGui.QHBoxLayout()
self.myQHBoxLayout.addWidget(self.myQPushButton)
self.setLayout(self.myQHBoxLayout)
self.myQPushButton.clicked.connect(self.addButtonClicked)
def addButtonClicked (self):
self.addButtonClickedSignal.emit(1)
class QMainWindow (QtGui.QMainWindow):
def __init__ (self, parent = None):
super(QMainWindow, self).__init__(parent)
myQRecordsDialog = QRecordsDialog(self)
myQRecordsDialog.addButtonClickedSignal.connect(self.showNewData)
myQRecordsDialog.show()
#QtCore.pyqtSlot(int)
def showNewData (self, signal):
if signal == 1:
print "It worked !"
else:
print "Problem ?"
myQApplication = QtGui.QApplication(sys.argv)
myQMainWindow = QMainWindow()
myQMainWindow.show()
sys.exit(myQApplication.exec_())
If you want to modify your PyQt code to use the PySide naming scheme, that can be done using a simple definition:
QtCore.Signal = QtCore.pyqtSignal
QtCore.Slot = QtCore.pyqtSlot
Reference this
Regards,
I am building a small GUI application which runs a producer (worker) and the GUI consumes the output on demand and plots it (using pyqtgraph).
Since the producer is a blocking function (takes a while to run), I (supposedly) moved it to its own thread.
When calling QThread.currentThreadId() from the producer it outputs the same number as the main GUI thread. So, the worker is executed first, and then all the plotting function calls are executed (because they are being queued on the same thread's event queue). How can I fix this?
Example run with partial:
gui thread id 140665453623104
worker thread id: 140665453623104
Here is my full code:
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import pyqtSignal
import pyqtgraph as pg
import numpy as np
from functools import partial
from Queue import Queue
import math
import sys
import time
class Worker(QtCore.QObject):
termino = pyqtSignal()
def __init__(self, q=None, parent=None):
super(Worker, self).__init__(parent)
self.q = q
def run(self, m=30000):
print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
for x in xrange(m):
#y = math.sin(x)
y = x**2
time.sleep(0.001) # Weird, plotting stops if this is not present...
self.q.put((x,y,y))
print('Worker finished')
self.termino.emit()
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.q = Queue()
self.termino = False
self.worker = Worker(self.q)
self.workerThread = None
self.btn = QtGui.QPushButton('Start worker')
self.pw = pg.PlotWidget(self)
pi = self.pw.getPlotItem()
pi.enableAutoRange('x', True)
pi.enableAutoRange('y', True)
self.ge1 = pi.plot(pen='y')
self.xs = []
self.ys = []
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.pw)
layout.addWidget(self.btn)
self.resize(400, 400)
def run(self):
self.workerThread = QtCore.QThread()
self.worker.moveToThread(self.workerThread)
self.worker.termino.connect(self.setTermino)
# moveToThread doesn't work here
self.btn.clicked.connect(partial(self.worker.run, 30000))
# moveToThread will work here
# assume def worker.run(self): instead of def worker.run(self, m=30000)
# self.btn.clicked.connect(self.worker.run)
self.btn.clicked.connect(self.graficar)
self.workerThread.start()
self.show()
def setTermino(self):
self.termino = True
def graficar(self):
if not self.q.empty():
e1,e2,ciclos = self.q.get()
self.xs.append(ciclos)
self.ys.append(e1)
self.ge1.setData(y=self.ys, x=self.xs)
if not self.termino:
QtCore.QTimer.singleShot(1, self.graficar)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
QtCore.QTimer.singleShot(0, window.run);
sys.exit(app.exec_())
The problem is that Qt attempts to choose the connection type (when you call signal.connect(slot)) based on the what thread the slot exists in. Because you have wrapped the slot in the QThread with partial, the slot you are connecting to resides in the MainThread (the GUI thread). You can override the connection type (as the second argument to connect() but that doesn't help because the method created by partial will always exist in the MainThread, and so setting the connection type to by Qt.QueuedConnection doesn't help.
The only way around this that I can see is to set up a relay signal, the sole purpose of which is to effectively change an emitted signal with no arguments (eg the clicked signal from a button) to a signal with one argument (your m parameter). This way you don't need to wrap the slot in the QThread with partial().
The code is below. I've created a signal with one argument (an int) called 'relay' in the main windows class. The button clicked signal is connected to a method within the main window class, and this method has a line of code which emits the custom signal I created. You can extend this method (relay_signal()) to get the integer to pass to the QThread as m (500 in this case), from where ever you like!
So here is the code:
from functools import partial
from Queue import Queue
import math
import sys
import time
class Worker(QtCore.QObject):
termino = pyqtSignal()
def __init__(self, q=None, parent=None):
super(Worker, self).__init__(parent)
self.q = q
def run(self, m=30000):
print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
for x in xrange(m):
#y = math.sin(x)
y = x**2
#time.sleep(0.001) # Weird, plotting stops if this is not present...
self.q.put((x,y,y))
print('Worker finished')
self.termino.emit()
class MainWindow(QtGui.QWidget):
relay = pyqtSignal(int)
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.q = Queue()
self.termino = False
self.worker = Worker(self.q)
self.workerThread = None
self.btn = QtGui.QPushButton('Start worker')
self.pw = pg.PlotWidget(self)
pi = self.pw.getPlotItem()
pi.enableAutoRange('x', True)
pi.enableAutoRange('y', True)
self.ge1 = pi.plot(pen='y')
self.xs = []
self.ys = []
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.pw)
layout.addWidget(self.btn)
self.resize(400, 400)
def run(self):
self.workerThread = QtCore.QThread()
self.worker.termino.connect(self.setTermino)
self.worker.moveToThread(self.workerThread)
# moveToThread doesn't work here
# self.btn.clicked.connect(partial(self.worker.run, 30000))
# moveToThread will work here
# assume def worker.run(self): instead of def worker.run(self, m=30000)
#self.btn.clicked.connect(self.worker.run)
self.relay.connect(self.worker.run)
self.btn.clicked.connect(self.relay_signal)
self.btn.clicked.connect(self.graficar)
self.workerThread.start()
self.show()
def relay_signal(self):
self.relay.emit(500)
def setTermino(self):
self.termino = True
def graficar(self):
if not self.q.empty():
e1,e2,ciclos = self.q.get()
self.xs.append(ciclos)
self.ys.append(e1)
self.ge1.setData(y=self.ys, x=self.xs)
if not self.termino or not self.q.empty():
QtCore.QTimer.singleShot(1, self.graficar)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
QtCore.QTimer.singleShot(0, window.run);
sys.exit(app.exec_())
I also modified the graficar method to continue plotting (even after the thread is terminated) if there is still data in the queue. I think this might be why you needed the time.sleep in the QThread, which is now also removed.
Also regarding your comments in the code on where to place moveToThread, where it is now is correct. It should be before the call that connects the QThread slot to a signal, and the reason for this is discussed in this stack-overflow post: PyQt: Connecting a signal to a slot to start a background operation