gpt4 book ai didi

python - 如何在不同类的 Main Widget 和 QObject 之间传递数据

转载 作者:行者123 更新时间:2023-12-01 00:18:08 28 4
gpt4 key购买 nike

我的问题是,如何在服务器和正在运行的 QWidget 应用程序之间传递数据?例如,当在 MyServer 中调用 onNewConnection(self) 函数时,我想在 UI 中显示一个新标签,显示“客户端已连接”。

任何帮助将不胜感激!

我有以下使用 Qt Websocket 服务器的 PyQt5 应用程序的程序结构:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QMainWindow, QPushButton, QVBoxLayout, QSystemTrayIcon, QStyle, QMenu, qApp, QAction
from PyQt5.QtGui import QIcon, QPixmap, QIcon, QFont
from PyQt5.QtCore import pyqtSlot, Qt, QObject, pyqtSignal
from PyQt5 import QtWebSockets, QtNetwork

import sys




class App(QWidget):
def __init__(self, *args, **kwargs):
super().__init__()

self.title = "Title window"
self.left = 0
self.top = 35
self.width = 550
self.height = 250

def init_ui(self):
# Initialise UI elements like buttons, labels, ...


class MyServer(QObject):

def __init__(self, parent):
super(QObject, self).__init__(parent)
self.clients = []
self.server = QtWebSockets.QWebSocketServer(parent.serverName(), parent.secureMode(), parent)
if self.server.listen(QtNetwork.QHostAddress.LocalHost, 8765):
print('Connected: '+self.server.serverName()+' : '+self.server.serverAddress().toString()+':'+str(self.server.serverPort()))
else:
print('error')
self.server.newConnection.connect(self.onNewConnection)

print(self.server.isListening())

def onNewConnection(self):
print("Connected")
#self.mySignal.emit("connected")
self.clientConnection = self.server.nextPendingConnection()
self.clientConnection.textMessageReceived.connect(self.processTextMessage)
self.clientConnection.binaryMessageReceived.connect(self.processBinaryMessage)
self.clientConnection.disconnected.connect(self.socketDisconnected)

self.clients.append(self.clientConnection)

def processTextMessage(self, message):
if (self.clientConnection):
self.clientConnection.sendTextMessage(message)
print(message)

def processBinaryMessage(self, message):
if (self.clientConnection):
self.clientConnection.sendBinaryMessage(message)

def socketDisconnected(self):
if (self.clientConnection):
self.clients.remove(self.clientConnection)
self.clientConnection.deleteLater()

app = QApplication(sys.argv)
ex = App()
serverObject = QtWebSockets.QWebSocketServer('My Socket', QtWebSockets.QWebSocketServer.NonSecureMode)
server = MyServer(serverObject)
serverObject.closed.connect(app.quit)
app.exec_()

最佳答案

Qt 推荐的最优雅的方法是使用信号和槽。

您的服务器也有一个错误,因为“clientConnection”指的是最后一个连接的套接字,因此如果它有n个“套接字”连接并且第一个接收信息,您将把它发送到最后一个似乎不正确的信息。最后你创建了QWebSocketServer,你只用它来存储名称和方式,这是没有意义的,而是最好将参数传递给构造函数

import sys

from PyQt5 import QtCore, QtWidgets, QtWebSockets, QtNetwork


class App(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.init_ui()

def init_ui(self):
self.status_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)

lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.status_label)

@QtCore.pyqtSlot(str)
def on_status_changed(self, status):
self.status_label.setText(status)


class MyServer(QtCore.QObject):
statusChanged = QtCore.pyqtSignal(str)

def __init__(self, name, mode, parent=None):
super().__init__(parent)
self.clients = []
self.server = QtWebSockets.QWebSocketServer(name, mode, parent)
self.server.closed.connect(QtCore.QCoreApplication.quit)
if self.server.listen(QtNetwork.QHostAddress.LocalHost, 8765):
print(
"Connected: {} : {} : {}".format(
self.server.serverName(),
self.server.serverAddress().toString(),
self.server.serverPort(),
)
)
else:
print("error: {}".format(self.server.errorString()))
self.server.newConnection.connect(self.onNewConnection)

print(self.server.isListening())

@QtCore.pyqtSlot()
def onNewConnection(self):
client = self.server.nextPendingConnection()
client.identifier = QtCore.QUuid.createUuid().toString(QtCore.QUuid.Id128)
client.textMessageReceived.connect(self.processTextMessage)
client.binaryMessageReceived.connect(self.processBinaryMessage)
client.disconnected.connect(self.socketDisconnected)
self.clients.append(client)
self.statusChanged.emit("Connected: client-{}".format(client.identifier))

@QtCore.pyqtSlot(str)
def processTextMessage(self, message):
client = self.sender()
if isinstance(client, QtWebSockets.QWebSocket):
client.sendTextMessage(message)
print("Client-{}: {}".format(client.identifier, message))

@QtCore.pyqtSlot(QtCore.QByteArray)
def processBinaryMessage(self, message):
client = self.sender()
if isinstance(client, QtWebSockets.QWebSocket):
client.sendBinaryMessage(message)
print("Client-{}: {}".format(client.identifier, message))

@QtCore.pyqtSlot()
def socketDisconnected(self):
client = self.sender()
if isinstance(client, QtWebSockets.QWebSocket):
self.clients.remove(client)
self.statusChanged.emit("Disconnected: client-{}".format(client.identifier))
client.deleteLater()


if __name__ == "__main__":

app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()

server = MyServer("My Socket", QtWebSockets.QWebSocketServer.NonSecureMode)
server.statusChanged.connect(ex.on_status_changed)

sys.exit(app.exec_())

关于python - 如何在不同类的 Main Widget 和 QObject 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59157478/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com