|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on 2023/02/23 |
| 5 | +@author: Irony |
| 6 | +@site: https://pyqt.site https://github.com/PyQt5 |
| 7 | +@email: 892768447@qq.com |
| 8 | +@file: CallInThread.py |
| 9 | +@description: |
| 10 | +""" |
| 11 | +import time |
| 12 | +from datetime import datetime |
| 13 | +from threading import Thread |
| 14 | + |
| 15 | +from PyQt5.QtCore import (Q_ARG, Q_RETURN_ARG, QMetaObject, Qt, QThread, |
| 16 | + pyqtSignal, pyqtSlot) |
| 17 | +from PyQt5.QtWidgets import QApplication, QHBoxLayout, QTextBrowser, QWidget |
| 18 | + |
| 19 | + |
| 20 | +class ThreadQt(QThread): |
| 21 | + |
| 22 | + def __init__(self, textBrowser, *args, **kwargs): |
| 23 | + super(ThreadQt, self).__init__(*args, **kwargs) |
| 24 | + self._textBrowser = textBrowser |
| 25 | + |
| 26 | + def stop(self): |
| 27 | + self.requestInterruption() |
| 28 | + |
| 29 | + def run(self): |
| 30 | + while not self.isInterruptionRequested(): |
| 31 | + text = datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 32 | + # 通过`invokeMethod`直接调用对应的槽函数 |
| 33 | + # 1. 获取函数`isReadOnly1`返回值, self.parent() 是Window窗口对象 |
| 34 | + # NOTE:注意这里获取返回值要用 `Qt.DirectConnection` 方式 |
| 35 | + retValue = QMetaObject.invokeMethod(self.parent(), 'isReadOnly1', |
| 36 | + Qt.DirectConnection, |
| 37 | + Q_RETURN_ARG(bool)) |
| 38 | + # 2. 通过`invokeMethod`队列调用对应控件槽函数`append` |
| 39 | + argValue = Q_ARG(str, text + ' readOnly: ' + str(retValue)) |
| 40 | + QMetaObject.invokeMethod(self._textBrowser, 'append', |
| 41 | + Qt.QueuedConnection, argValue) |
| 42 | + self.sleep(1) |
| 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): |
| 71 | + |
| 72 | + # 更新信号 |
| 73 | + appendText = pyqtSignal(str) |
| 74 | + |
| 75 | + def __init__(self, *args, **kwargs): |
| 76 | + super(Window, self).__init__(*args, **kwargs) |
| 77 | + layout = QHBoxLayout(self) |
| 78 | + self.textBrowser1 = QTextBrowser(self) |
| 79 | + self.textBrowser2 = QTextBrowser(self) |
| 80 | + layout.addWidget(self.textBrowser1) |
| 81 | + layout.addWidget(self.textBrowser2) |
| 82 | + |
| 83 | + self.appendText.connect(self.textBrowser2.append) |
| 84 | + |
| 85 | + # Qt线程 |
| 86 | + self.thread1 = ThreadQt(self.textBrowser1, self) |
| 87 | + self.thread1.start() |
| 88 | + |
| 89 | + # PY线程 |
| 90 | + self.thread2 = ThreadPy(self.textBrowser2, self) |
| 91 | + self.thread2.start() |
| 92 | + |
| 93 | + @pyqtSlot(result=bool) |
| 94 | + def isReadOnly1(self): |
| 95 | + # 线程中直接调用该槽函数获取UI中的内容 |
| 96 | + return self.textBrowser1.isReadOnly() |
| 97 | + |
| 98 | + def closeEvent(self, event): |
| 99 | + self.thread1.stop() |
| 100 | + self.thread2.stop() |
| 101 | + self.thread1.wait() |
| 102 | + self.thread2.join() |
| 103 | + super(Window, self).closeEvent(event) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + import cgitb |
| 108 | + import sys |
| 109 | + |
| 110 | + cgitb.enable(format='text') |
| 111 | + app = QApplication(sys.argv) |
| 112 | + w = Window() |
| 113 | + w.show() |
| 114 | + sys.exit(app.exec_()) |
0 commit comments