实时追加显示

  • 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

clipboard.png

目前遇到的问题是qt designer使用上遇到很多坑,像是菜单,工具栏各个按钮之间怎么去绑定特定的槽,各种报错.

然后现在想到一种方式是,qt designer纯用来设计页面,信号与槽之间的绑定自己写,修修补补.

参考


EngineerLeo
598 声望38 粉丝

专注于云原生、AI等相关技术