但是照搬原代码,会提示
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()
代码无法执行,窗口直接销毁,无任何报错