python中logging内容展示在pyqt多线程问题

stackoverflow上找到了logging的处理

但是照搬原代码,会提示

QObject::connect: Cannot queue arguments of type 'QTextBlock'
(Make sure 'QTextBlock' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)

照着去搜索,找到了较为安全的修改用户界面的方式是向UI窗口发送信号signal

综合一下,代码如下


class MyThead(QtCore.QThread):
    updated = QtCore.pyqtSignal(str)
    
    def __init__(self, mainUI, msg):
        super().__init__()
        self.updated.connect(mainUI.plainTextEdit.appendPlainText)
        self.msg = msg

    def run(self):
        self.updated.emit(self.msg)

class QPlainTextEditLogger(logging.Handler):

    def __init__(self, mainUI):
        super().__init__()
        self.mainUI = mainUI

    def emit(self, record):
        msg = self.format(record)
        _thread = MyThead(self.mainUI, msg)
        _thread.start()

代码无法执行,窗口直接销毁,无任何报错

阅读 5.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏