PyQt5+eric6之旅(三) - 多线程应用 - 追加显示
实时追加显示
code
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import * from PyQt5.QtWidgets import * import time import sys class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号 def write(self, data): self.textWritten.emit(str(data)) class BackendThread(QThread): # 通过类成员对象定义信号 update_date = pyqtSignal(str) # 处理业务逻辑 def run(self): while True: data = QDateTime.currentDateTime() currTime = data.toString("yyyy-MM-dd hh:mm:ss") self.update_date.emit( str(currTime) ) time.sleep(1) 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.gridLayout = QtWidgets.QGridLayout(self.centralWidget) self.gridLayout.setObjectName("gridLayout") self.pushButton = QtWidgets.QPushButton(self.centralWidget) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1) self.textEdit = QtWidgets.QTextEdit(self.centralWidget) self.textEdit.setObjectName("textEdit") self.gridLayout.addWidget(self.textEdit, 1, 0, 1, 1) MainWindow.setCentralWidget(self.centralWidget) #下面将输出重定向到textEdit中 sys.stdout = EmittingStream(textWritten=self.handleDisplay) sys.stderr = EmittingStream(textWritten=self.handleDisplay) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "实时显示")) self.pushButton.setText(_translate("MainWindow", "开始")) def initUI(self): # 创建线程 self.backend = BackendThread() # 连接信号 self.backend.update_date.connect(self.handleDisplay) # 开始线程 self.backend.start() # 将当前时间输出到文本框 def handleDisplay(self, data): cursor = self.textEdit.textCursor() cursor.movePosition(QtGui.QTextCursor.End) # self.textEdit.setText(data) cursor.insertText(data + '\n') self.textEdit.setTextCursor(cursor) self.textEdit.ensureCursorVisible() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() win = Ui_MainWindow() win.setupUi(MainWindow) win.initUI() MainWindow.show() sys.exit(app.exec_())
- result
目前遇到的问题是qt designer使用上遇到很多坑,像是菜单,工具栏各个按钮之间怎么去绑定特定的槽,各种报错.
然后现在想到一种方式是,qt designer纯用来设计页面,信号与槽之间的绑定自己写,修修补补.
参考
相关推荐
meylovezn 2020-09-15
zhongfuyu 2020-08-16
JimyFengqi 2020-08-16
追逐阳光的风 2020-07-04
hustlei 2020-06-21
lianback 2020-06-21
王磊的程序员之路 2020-06-21
kikaylee 2020-06-12
生物信息学 2020-06-07
hustlei 2020-06-04
zhongfuyu 2020-05-30
hustlei 2020-05-19
zhongfuyu 2020-05-19
zhongfuyu 2020-05-14
JasonYeung 2020-05-09