| 43 | |
| 44 | |
| 45 | class ThreadPy(Thread): |
| 46 | |
| 47 | def __init__(self, textBrowser, parent, *args, **kwargs): |
| 48 | super(ThreadPy, self).__init__(*args, **kwargs) |
| 49 | self._running = True |
| 50 | self._textBrowser = textBrowser |
| 51 | self._parent = parent |
| 52 | |
| 53 | def stop(self): |
| 54 | self._running = False |
| 55 | |
| 56 | def run(self): |
| 57 | while self._running: |
| 58 | text = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 59 | # 通过`invokeMethod`队列调用对应控件信号,`self._parent`是Window窗口对象 |
| 60 | QMetaObject.invokeMethod(self._parent, 'appendText', |
| 61 | Qt.QueuedConnection, |
| 62 | Q_ARG(str, text + ' from Signal')) |
| 63 | # 通过`invokeMethod`队列调用对应控件槽函数`append` |
| 64 | QMetaObject.invokeMethod(self._textBrowser, 'append', |
| 65 | Qt.QueuedConnection, |
| 66 | Q_ARG(str, text + ' to Slot')) |
| 67 | time.sleep(1) |
| 68 | |
| 69 | |
| 70 | class Window(QWidget): |