diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index ce12414..160c28e 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -68,5 +68,7 @@ encoding//QWidget/Lib/CustomPaintWidget.py=utf-8 encoding//QWidget/Lib/CustomWidget.py=utf-8 encoding//QWidget/WidgetStyle.py=utf-8 encoding//QtQuick/FlatStyle.py=utf-8 +encoding//QtRemoteObjects/SyncUi/WindowMaster.py=utf-8 +encoding//QtRemoteObjects/SyncUi/WindowSlave.py=utf-8 encoding//Test/\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8 encoding//Test/\u81EA\u52A8\u66F4\u65B0/test.py=utf-8 diff --git a/QtRemoteObjects/README.md b/QtRemoteObjects/README.md index e76b0cf..cde710a 100644 --- a/QtRemoteObjects/README.md +++ b/QtRemoteObjects/README.md @@ -14,4 +14,11 @@ [运行 registryconnecteddynamicserver.py](simpleswitch/registryconnecteddynamicserver.py) | [运行 registryconnecteddynamicclient.py](simpleswitch/registryconnecteddynamicclient.py) -官方关于简单的信号槽、属性访问测试例子 \ No newline at end of file +官方关于简单的信号槽、属性访问测试例子 + +## 3、简单界面数据同步 +[运行 WindowMaster.py](SyncUi/WindowMaster.py) | [运行 WindowSlave.py](SyncUi/WindowSlave.py) + +绑定信号槽同步双方数据,属性方法测试没通过,详细注释在代码中 + +![SyncUi](ScreenShot/SyncUi.gif) \ No newline at end of file diff --git a/QtRemoteObjects/ScreenShot/SyncUi.gif b/QtRemoteObjects/ScreenShot/SyncUi.gif new file mode 100644 index 0000000..da832bb Binary files /dev/null and b/QtRemoteObjects/ScreenShot/SyncUi.gif differ diff --git a/QtRemoteObjects/SyncUi/WindowMaster.py b/QtRemoteObjects/SyncUi/WindowMaster.py new file mode 100644 index 0000000..22ba0bc --- /dev/null +++ b/QtRemoteObjects/SyncUi/WindowMaster.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2019年8月7日 +@author: Irony +@site: https://pyqt5.com https://github.com/892768447 +@email: 892768447@qq.com +@file: QtRemoteObjects.SyncUi.WindowMaster +@description: 主窗口 +""" +from PyQt5.QtCore import QUrl, QTimer, pyqtSignal, pyqtSlot +from PyQt5.QtRemoteObjects import QRemoteObjectHost +from PyQt5.QtWidgets import QWidget, QLineEdit, QVBoxLayout, QCheckBox,\ + QProgressBar + + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2019 Irony' +__Version__ = 1.0 + + +class WindowMaster(QWidget): + + # 输入框内容变化信号 + editValueChanged = pyqtSignal(str) + # 勾选框变化信号 + checkToggled = pyqtSignal(bool) + # 进度条变化信号 + progressValueChanged = pyqtSignal(int) + + def __init__(self, *args, **kwargs): + super(WindowMaster, self).__init__(*args, **kwargs) + self.setupUi() + + # 开启节点 + host = QRemoteObjectHost(QUrl('local:WindowMaster'), parent=self) + host.enableRemoting(self, 'WindowMaster') + print('开启节点完成') + + # 定时器更新进度条 + self._value = 0 + self.utimer = QTimer(self, timeout=self.updateProgress) + self.utimer.start(200) + + def setupUi(self): + self.setWindowTitle('WindowMaster') + self.resize(300, 400) + layout = QVBoxLayout(self) + # 输入框(双向同步) + self.lineEdit = QLineEdit(self) + self.lineEdit.textChanged.connect(self.editValueChanged.emit) + # 勾选框(双向同步) + self.checkBox = QCheckBox('来勾我啊', self) + self.checkBox.toggled.connect(self.checkToggled.emit) + # 进度条(Master更新Slave) + self.progressBar = QProgressBar(self) + self.progressBar.valueChanged.connect(self.progressValueChanged.emit) + layout.addWidget(self.lineEdit) + layout.addWidget(self.checkBox) + layout.addWidget(self.progressBar) + + def updateProgress(self): + self._value += 1 + if self._value > 100: + self._value = 0 + self.progressBar.setValue(self._value) + + @pyqtSlot(str) + def updateEdit(self, text): + """更新输入框内容的槽函数 + :param text: + """ + self.lineEdit.setText(text) + + @pyqtSlot(bool) + def updateCheck(self, checked): + """更新勾选框的槽函数 + :param checked: + """ + self.checkBox.setChecked(checked) + + +if __name__ == '__main__': + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = WindowMaster() + w.show() + sys.exit(app.exec_()) diff --git a/QtRemoteObjects/SyncUi/WindowSlave.py b/QtRemoteObjects/SyncUi/WindowSlave.py new file mode 100644 index 0000000..44cfc14 --- /dev/null +++ b/QtRemoteObjects/SyncUi/WindowSlave.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2019年8月7日 +@author: Irony +@site: https://pyqt5.com https://github.com/892768447 +@email: 892768447@qq.com +@file: QtRemoteObjects.SyncUi.WindowSlave +@description: 备窗口 +""" +from PyQt5.QtCore import QUrl +from PyQt5.QtRemoteObjects import QRemoteObjectNode, QRemoteObjectReplica +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLineEdit, QCheckBox,\ + QProgressBar, QMessageBox + + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2019 Irony' +__Version__ = 1.0 + + +class WindowSlave(QWidget): + + def __init__(self, *args, **kwargs): + super(WindowSlave, self).__init__(*args, **kwargs) + self.setupUi() + # 加入Master节点 + node = QRemoteObjectNode(parent=self) + node.connectToNode(QUrl('local:WindowMaster')) + # 获取WindowMaster对象 + self.windowMaster = node.acquireDynamic('WindowMaster') + # 初始化成功后才能去绑定信号等 + self.windowMaster.initialized.connect(self.onInitialized) + # 状态改变 https://doc.qt.io/qt-5/qremoteobjectreplica.html#State-enum + self.windowMaster.stateChanged.connect(self.onStateChanged) + + def setupUi(self): + self.setWindowTitle('WindowSlave') + self.resize(300, 400) + layout = QVBoxLayout(self) + # 输入框(双向同步) + self.lineEdit = QLineEdit(self) + # 勾选框(双向同步) + self.checkBox = QCheckBox('来勾我啊', self) + # 进度条(Master更新Slave) + self.progressBar = QProgressBar(self) + layout.addWidget(self.lineEdit) + layout.addWidget(self.checkBox) + layout.addWidget(self.progressBar) + + def onStateChanged(self, newState, oldState): + if newState == QRemoteObjectReplica.Suspect: + QMessageBox.critical(self, '错误', '连接丢失') + + def onInitialized(self): + # Master和Slave输入框绑定 + self.windowMaster.editValueChanged.connect(self.lineEdit.setText) + self.lineEdit.textChanged.connect(self.windowMaster.updateEdit) + + # Master和Slave勾选框绑定 + self.windowMaster.checkToggled.connect(self.checkBox.setChecked) + self.checkBox.toggled.connect(self.windowMaster.updateCheck) + + # Master进度条同步到Slave + self.windowMaster.progressValueChanged.connect( + self.progressBar.setValue) + + print('绑定信号槽完成') + + +if __name__ == '__main__': + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = WindowSlave() + w.show() + sys.exit(app.exec_()) diff --git a/README.md b/README.md index b591549..85a8421 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站 - [折叠动画](Test/partner_625781186/2.折叠控件) - [RemoteObjects](QtRemoteObjects) + - [简单界面数据同步](QtRemoteObjects/SyncUi) - [modelview](QtRemoteObjects/modelview) - [simpleswitch](QtRemoteObjects/simpleswitch)