我正在创建一些按钮并为其分配单击事件。每个按钮都分配给一个可以输入数据的对话窗口。输入数据后,将其发送到 Flask 服务器,Flask 服务器将该数据发送回客户端 Python 应用程序。
在套接字事件中,我断开按钮并连接新的单击事件。断开连接似乎工作正常,但再次连接按钮似乎不起作用。
我正在创建一些按钮并为其分配单击事件。每个按钮都分配给一个可以输入数据的对话窗口。输入数据后,将其发送到 Flask 服务器,Flask 服务器将该数据发送回客户端 Python 应用程序。
在套接字事件中,我断开按钮并连接新的单击事件。断开连接似乎工作正常,但再次连接按钮似乎不起作用。
def create_flow_layout(self)
""" This is where I create the buttons"""
# Looping through a list of locations
for location in self.locations:
self.data[location] = {
# Keys/values which hold the button and information related
to the button
}
button = QPushButton(location)
button.clicked.connect(lambda checked, loc=location:
self.open_dialog_logged_pallet(loc))
def open_dialog_logged_pallet(self, location)
""" This is where I open a dialog which is related to the button"""
# Basically just a few line edit's that are related to the location/button
# After the line edits are submitted the button data is posted to a flask server and the flask server emits the changes
def update_buttons(self, data):
""" This is connected to the socket event that the flask server
emits after the data from open_dialog_logged_pallet is sent to it"""
# THIS IS WHERE I WAS HAVING ISSUES - The button would disconnect,
but the new clicked event was not applied
for location, values in data['data'].items():
button = self.data[location]['button']
button.clicked.disconnect()
button.clicked.connect(lambda checked, loc=location: self.open_dialog_logged_pallet(loc))
我尝试尝试不同的东西。我考虑过不允许 Flask 服务器向自身发出信号,但如果能让这个功能正常工作那就太好了。
所以我最终不得不创建一个信号来检测数据何时更新。
class ReceivingManager(QObject):
dataObjChanged = Signal(dict) # Signal to emit when data_obj changes
def __init__(self, ui, api):
super().__init__()
self.data = {}
self._data_obj = {} # Initialize _data_obj before using the property setter
self.data_obj = {} # Now this uses the setter and is safe
self.create_flow_layout()
self.dataObjChanged.connect(self.handle_data_change)
@property
def data_obj(self):
return self._data_obj
@data_obj.setter
def data_obj(self, value):
if self._data_obj != value:
self._data_obj = value
self.dataObjChanged.emit(self._data_obj) # Emit signal when data_obj changes
def create_flow_layout(self)
""" This is where I create the buttons"""
# Looping through a list of locations
for location in self.locations:
self.data[location] = {
# Keys/values which hold the button and information related
to the button
}
button = QPushButton(location)
button.clicked.connect(lambda checked, loc=location:
self.open_dialog_logged_pallet(loc))
def update_buttons(self, data):
# I now update self.data_obj with the data received from the flask server
def handle_data_change(self, data):
# Since I updated the self.data_obj in update_buttons, this gets called because we setup a signal to call this whenever self.data_obj is updated
# Now i'm able to successfully disconnect, and reconnect the buttons how I want. I use the location in the data here to reference the item I want within self.data