PyQt5 Signals and Threading

2024/11/14 16:21:41

I watched a short tutorial on PyQt4 signals on youtube and am having trouble getting a small sample program running. How do I connect my signal being emitted from a thread to the main window?

import cpuUsageGui
import sys
import sysInfo
from PyQt5 import QtCore"""Main window setup"""
app = cpuUsageGui.QtWidgets.QApplication(sys.argv)
Form = cpuUsageGui.QtWidgets.QWidget()
ui = cpuUsageGui.Ui_Form()
ui.setupUi(Form)def updateProgBar(val):ui.progressBar.setValue(val)class ThreadClass(QtCore.QThread):def run(self):while True:val = sysInfo.getCpu()self.emit(QtCore.pyqtSignal('CPUVALUE'), val)threadclass = ThreadClass()# This section does not work
connect(threadclass, QtCore.pyqtSignal('CPUVALUE'), updateProgBar)
# This section does not workif __name__ == "__main__":threadclass.start()Form.show()sys.exit(app.exec_())
Answer

The signal must be created, inside your ThreadClass, or before but as you emit the signal inside the ThreadClass, it is better to create it inside your class.

After creation, you need to connect it to the progress bar function. Here is an example of the signal created and connected inside your class.

class ThreadClass(QtCore.QThread):# Create the signalsig = QtCore.pyqtSignal(int)def __init__(self, parent=None):super(ThreadClass, self).__init__(parent)# Connect signal to the desired functionself.sig.connect(updateProgBar)def run(self):while True:val = sysInfo.getCpu()# Emit the signalself.sig.emit(val)

Keep in mind that signals have changed style since PyQt5 : Description

if you watched a tutorial for PyQt4, it is not be the same.

https://en.xdnf.cn/q/71998.html

Related Q&A

Pythons hasattr sometimes returns incorrect results

Why does hasattr say that the instance doesnt have a foo attribute?>>> class A(object): ... @property ... def foo(self): ... ErrorErrorError ... >>> a = A() >>…

Pure python library to read and write jpeg format

guys! Im looking for pure python implementation of jpeg writing (reading will be nice, but not necessary) library. Ive founded only TonyJPEG library port at http://mail.python.org/pipermail/image-sig/2…

Conda - unable to completely delete environment

I am using Windows 10 (all commands run as administrator). I created an environment called myenv. Then I used conda env remove -n myenvNow, if I tryconda info --envsI only see the base environment. How…

How to list all function names of a Python module in C++?

I have a C++ program, I want to import a Python module and list all function names in this module. How can I do it?I used the following code to get the dict from a module:PyDictObject* pDict = (PyDict…

Pandas groupby and sum total of group

I have a Pandas DataFrame with customer refund reasons. It contains these example data rows:**case_type** **claim_type** 1 service service 2 service service 3 charg…

Capture webcam video using PyQt

Given the following PyQt code, I can perfectly capture the webcams streaming video. Now, I want to modify code, so a button named capture button is added that once pressed captures the streaming video …

Plot a 3d surface from a list of lists using matplotlib

Ive searched around for a bit, and whhile I can find many useful examples of meshgrid, none shhow clearly how I can get data from my list of lists into an acceptable form for any of the varied ways Ive…

Super fast way to compare if two strings are equal

Obviously, in Python to check whether two strings are equal you can do:"hello word" == "hello world"But what if you are comparing really long strings (in excess of 1m characters)? …

Pandas DataFrames in reportlab

I have a DataFrame, and want to output it to a pdf. Im currently trying to use ReportLab for this, but it wont seem to work. I get an error here:mytable = Table(make_pivot_table(data, pivot_cols, colum…

How to open and close a website using default browser with python

Im trying to write a python script on windows platform to open a webpage(such as Google), and then, after 10 seconds, close this website. Note: Im using Windows 7, Python 2.7.10, and IE