PYQT 子线程调用GUI学习记录—添加文档到textbrowser
- 在类中定义信号,定义参数类型
- 在构造函数中将信号连接到将要执行的函数
- 在要调用的函数中使用self._signal.emit(text)发送信号,text为将要传递的数据
- 具体执行添加任务的函数myadd
以下是自己学习过程中找到的代码
from PyQt5 import QtCore, QtGui, QtWidgets
from Ui_mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
_signal=QtCore.pyqtSignal(str) #定义信号,定义参数为str类型
def __init__(self):
super(MainWindow,self).__init__()
self.setupUi(self)
self.myButton.clicked.connect(self.myPrint)
self._signal.connect(self.myAdd) #将信号连接到函数mySignal
def myPrint(self,text):
self.textBrowser.setText("")
self.textBrowser.append("正在打印,请稍候")