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 __init__(self, *args, **kwargs): super(WindowSlave, self).__init__(*args, **kwargs) # 监听剪切板 clipboard = QApplication.clipboard() clipboard.dataChanged.connect(self.on_data_changed) # 加入Master节点 node = QRemoteObjectNode(parent=self) node.connectToNode(QUrl('tcp://{}:{}'.format(sys.argv[1], sys.argv[2]))) # 获取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 on_buttonConnect_clicked(self): # 连接 url = self.editAddress.text().strip() if not url or not url.startswith('tcp://'): return self.editAddress.setEnabled(False) self.buttonConnect.setEnabled(False) # 加入Master节点 node = QRemoteObjectNode(parent=self) node.connectToNode(QUrl(url)) # 获取远程对象 self._socket = node.acquireDynamic('QtDesignerPluginWebSocket') if not self._socket: self.onDisconnected() self.textBrowserApi.append('<font color=red>{0}</font>'.format( self.tr('无法连接: %s' % url))) return # 状态改变 https://doc.qt.io/qt-5/qremoteobjectreplica.html#State-enum self._socket.stateChanged.connect(self.onStateChanged) # 初始化成功后才能去绑定信号等 self._socket.initialized.connect(self.onInitialized)
# Emit the signal to echo the received state back to the server. self.echoSwitchState.emit(self._clientSwitchState) @pyqtSlot() def initConnection(self): # Connect the replica source signal currStateChanged() with the # client's recSwitchState() slot to receive the source's current state. self._replica.currStateChanged.connect(self.recSwitchState) # Connect the client's echoSwitchState() signal with replica's # server_slot() to echo back the received state. self.echoSwitchState.connect(self._replica.server_slot) if __name__ == '__main__': app = QCoreApplication(sys.argv) # Create the remote object node. repNode = QRemoteObjectNode() # Connect with the remote host node. repNode.connectToNode(QUrl('local:replica')) # Acquire a replica of the source from the host node. replica = repNode.acquireDynamic('SimpleSwitch') # Create the client switch object and pass the replica to it. rswitch = DynamicClient(replica) sys.exit(app.exec_())
self.echoSwitchState.emit(self._clientSwitchState) @pyqtSlot() def initConnection(self): # Connect the replica source signal currStateChanged() with the # client's recSwitchState() slot to receive the source's current state. self._replica.currStateChanged.connect(self.recSwitchState) # Connect the client's echoSwitchState() signal with replica's # server_slot() to echo back the received state. self.echoSwitchState.connect(self._replica.server_slot) if __name__ == "__main__": app = QCoreApplication(sys.argv) # Create the remote object node. repNode = QRemoteObjectNode() # Connect with the remote host node. repNode.connectToNode(QUrl("local:replica")) # Acquire a replica of the source from the host node. replica = repNode.acquireDynamic("SimpleSwitch") # Create the client switch object and pass the replica to it. rswitch = DynamicClient(replica) sys.exit(app.exec_())