我正在尝试使用QThread和信号制作进度条。 但是我无法将带有进度的信号发送回GUI类:
class RealTh(QThread):
value = pyqtSignal(int)
result = pyqtSignal(list)
def run(self):
x = calc().longtask()[0]
self.value.emit(x)
l = calc().longtask()[1]
self.result.emit(l)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
...
...
self.bcalc.clicked.connect(self.v2)
...
def v2(self):
self.thread = RealTh()
self.thread.value.connect(self.setProgressVal)
self.thread.result.connect(self.printer)
self.thread.start()
def printer(self, list):
print(list)
def setProgressVal(self, val):
self.progressBar.setValue(val)
class calc():
def __init__(self):
self.calc = calc
def longtask(self):
print('task started')
sleep(10)
RealTh().value.emit(10)
print('done')
return [42, ['hello', 'this', 'is', 'result']]
如上所述,我正在尝试从emit
函数中获取value
的信号calc.longtask()
。但是,当我这样做时,我得到:
'RealTh' does not have a signal with the signature value(int)
我知道this,但是情况不一样。
对于那些想重现此内容的人,下面是完整的CPed代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import fakecalc
from time import sleep
from PyQt5.QtCore import Qt, QThread, pyqtSignal
class RealTh(QThread):
value = pyqtSignal(int)
result = pyqtSignal(list)
def run(self):
x = calc().longtask()[0]
self.value.emit(x)
l = calc().longtask()[1]
self.result.emit(l)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(70, 370, 641, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.bprog = QtWidgets.QPushButton(self.centralwidget)
self.bprog.setGeometry(QtCore.QRect(160, 170, 101, 51))
self.bprog.setObjectName("bprog")
self.bcalc = QtWidgets.QPushButton(self.centralwidget)
self.bcalc.setGeometry(QtCore.QRect(500, 170, 101, 51))
self.bcalc.setObjectName("bcalc")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.bcalc.clicked.connect(self.v2)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.bprog.setText(_translate("MainWindow", "Start prog bar"))
self.bcalc.setText(_translate("MainWindow", "Start calc"))
def v2(self):
self.thread = RealTh()
self.thread.value.connect(self.setProgressVal)
self.thread.result.connect(self.printer)
self.thread.start()
def printer(self, list):
print(list)
def setProgressVal(self, val):
self.progressBar.setValue(val)
class calc():
def __init__(self):
self.calc = calc
def longtask(self):
print('task started')
RealTh().value.emit(10)
print('done')
return [42, ['hello', 'this', 'is', 'result']]
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())