Skip to content

Commit 5e0ada1

Browse files
committed
简单界面数据同步
1 parent f2d5bc5 commit 5e0ada1

File tree

6 files changed

+179
-1
lines changed

6 files changed

+179
-1
lines changed

.settings/org.eclipse.core.resources.prefs

+2
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,7 @@ encoding//QWidget/Lib/CustomPaintWidget.py=utf-8
6868
encoding//QWidget/Lib/CustomWidget.py=utf-8
6969
encoding//QWidget/WidgetStyle.py=utf-8
7070
encoding//QtQuick/FlatStyle.py=utf-8
71+
encoding//QtRemoteObjects/SyncUi/WindowMaster.py=utf-8
72+
encoding//QtRemoteObjects/SyncUi/WindowSlave.py=utf-8
7173
encoding//Test/\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8
7274
encoding//Test/\u81EA\u52A8\u66F4\u65B0/test.py=utf-8

QtRemoteObjects/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414

1515
[运行 registryconnecteddynamicserver.py](simpleswitch/registryconnecteddynamicserver.py) | [运行 registryconnecteddynamicclient.py](simpleswitch/registryconnecteddynamicclient.py)
1616

17-
官方关于简单的信号槽、属性访问测试例子
17+
官方关于简单的信号槽、属性访问测试例子
18+
19+
## 3、简单界面数据同步
20+
[运行 WindowMaster.py](SyncUi/WindowMaster.py) | [运行 WindowSlave.py](SyncUi/WindowSlave.py)
21+
22+
绑定信号槽同步双方数据,属性方法测试没通过,详细注释在代码中
23+
24+
![SyncUi](ScreenShot/SyncUi.gif)

QtRemoteObjects/ScreenShot/SyncUi.gif

270 KB
Loading
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2019年8月7日
6+
@author: Irony
7+
@site: https://pyqt5.com https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: QtRemoteObjects.SyncUi.WindowMaster
10+
@description: 主窗口
11+
"""
12+
from PyQt5.QtCore import QUrl, QTimer, pyqtSignal, pyqtSlot
13+
from PyQt5.QtRemoteObjects import QRemoteObjectHost
14+
from PyQt5.QtWidgets import QWidget, QLineEdit, QVBoxLayout, QCheckBox,\
15+
QProgressBar
16+
17+
18+
__Author__ = 'Irony'
19+
__Copyright__ = 'Copyright (c) 2019 Irony'
20+
__Version__ = 1.0
21+
22+
23+
class WindowMaster(QWidget):
24+
25+
# 输入框内容变化信号
26+
editValueChanged = pyqtSignal(str)
27+
# 勾选框变化信号
28+
checkToggled = pyqtSignal(bool)
29+
# 进度条变化信号
30+
progressValueChanged = pyqtSignal(int)
31+
32+
def __init__(self, *args, **kwargs):
33+
super(WindowMaster, self).__init__(*args, **kwargs)
34+
self.setupUi()
35+
36+
# 开启节点
37+
host = QRemoteObjectHost(QUrl('local:WindowMaster'), parent=self)
38+
host.enableRemoting(self, 'WindowMaster')
39+
print('开启节点完成')
40+
41+
# 定时器更新进度条
42+
self._value = 0
43+
self.utimer = QTimer(self, timeout=self.updateProgress)
44+
self.utimer.start(200)
45+
46+
def setupUi(self):
47+
self.setWindowTitle('WindowMaster')
48+
self.resize(300, 400)
49+
layout = QVBoxLayout(self)
50+
# 输入框(双向同步)
51+
self.lineEdit = QLineEdit(self)
52+
self.lineEdit.textChanged.connect(self.editValueChanged.emit)
53+
# 勾选框(双向同步)
54+
self.checkBox = QCheckBox('来勾我啊', self)
55+
self.checkBox.toggled.connect(self.checkToggled.emit)
56+
# 进度条(Master更新Slave)
57+
self.progressBar = QProgressBar(self)
58+
self.progressBar.valueChanged.connect(self.progressValueChanged.emit)
59+
layout.addWidget(self.lineEdit)
60+
layout.addWidget(self.checkBox)
61+
layout.addWidget(self.progressBar)
62+
63+
def updateProgress(self):
64+
self._value += 1
65+
if self._value > 100:
66+
self._value = 0
67+
self.progressBar.setValue(self._value)
68+
69+
@pyqtSlot(str)
70+
def updateEdit(self, text):
71+
"""更新输入框内容的槽函数
72+
:param text:
73+
"""
74+
self.lineEdit.setText(text)
75+
76+
@pyqtSlot(bool)
77+
def updateCheck(self, checked):
78+
"""更新勾选框的槽函数
79+
:param checked:
80+
"""
81+
self.checkBox.setChecked(checked)
82+
83+
84+
if __name__ == '__main__':
85+
import sys
86+
from PyQt5.QtWidgets import QApplication
87+
app = QApplication(sys.argv)
88+
w = WindowMaster()
89+
w.show()
90+
sys.exit(app.exec_())

QtRemoteObjects/SyncUi/WindowSlave.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2019年8月7日
6+
@author: Irony
7+
@site: https://pyqt5.com https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: QtRemoteObjects.SyncUi.WindowSlave
10+
@description: 备窗口
11+
"""
12+
from PyQt5.QtCore import QUrl
13+
from PyQt5.QtRemoteObjects import QRemoteObjectNode, QRemoteObjectReplica
14+
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLineEdit, QCheckBox,\
15+
QProgressBar, QMessageBox
16+
17+
18+
__Author__ = 'Irony'
19+
__Copyright__ = 'Copyright (c) 2019 Irony'
20+
__Version__ = 1.0
21+
22+
23+
class WindowSlave(QWidget):
24+
25+
def __init__(self, *args, **kwargs):
26+
super(WindowSlave, self).__init__(*args, **kwargs)
27+
self.setupUi()
28+
# 加入Master节点
29+
node = QRemoteObjectNode(parent=self)
30+
node.connectToNode(QUrl('local:WindowMaster'))
31+
# 获取WindowMaster对象
32+
self.windowMaster = node.acquireDynamic('WindowMaster')
33+
# 初始化成功后才能去绑定信号等
34+
self.windowMaster.initialized.connect(self.onInitialized)
35+
# 状态改变 https://doc.qt.io/qt-5/qremoteobjectreplica.html#State-enum
36+
self.windowMaster.stateChanged.connect(self.onStateChanged)
37+
38+
def setupUi(self):
39+
self.setWindowTitle('WindowSlave')
40+
self.resize(300, 400)
41+
layout = QVBoxLayout(self)
42+
# 输入框(双向同步)
43+
self.lineEdit = QLineEdit(self)
44+
# 勾选框(双向同步)
45+
self.checkBox = QCheckBox('来勾我啊', self)
46+
# 进度条(Master更新Slave)
47+
self.progressBar = QProgressBar(self)
48+
layout.addWidget(self.lineEdit)
49+
layout.addWidget(self.checkBox)
50+
layout.addWidget(self.progressBar)
51+
52+
def onStateChanged(self, newState, oldState):
53+
if newState == QRemoteObjectReplica.Suspect:
54+
QMessageBox.critical(self, '错误', '连接丢失')
55+
56+
def onInitialized(self):
57+
# Master和Slave输入框绑定
58+
self.windowMaster.editValueChanged.connect(self.lineEdit.setText)
59+
self.lineEdit.textChanged.connect(self.windowMaster.updateEdit)
60+
61+
# Master和Slave勾选框绑定
62+
self.windowMaster.checkToggled.connect(self.checkBox.setChecked)
63+
self.checkBox.toggled.connect(self.windowMaster.updateCheck)
64+
65+
# Master进度条同步到Slave
66+
self.windowMaster.progressValueChanged.connect(
67+
self.progressBar.setValue)
68+
69+
print('绑定信号槽完成')
70+
71+
72+
if __name__ == '__main__':
73+
import sys
74+
from PyQt5.QtWidgets import QApplication
75+
app = QApplication(sys.argv)
76+
w = WindowSlave()
77+
w.show()
78+
sys.exit(app.exec_())

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
169169
- [折叠动画](Test/partner_625781186/2.折叠控件)
170170

171171
- [RemoteObjects](QtRemoteObjects)
172+
- [简单界面数据同步](QtRemoteObjects/SyncUi)
172173
- [modelview](QtRemoteObjects/modelview)
173174
- [simpleswitch](QtRemoteObjects/simpleswitch)
174175

0 commit comments

Comments
 (0)