python - QListWidget:currentRowChanged 回滚
问题描述
def selected_radio_connection_changed(self,current_row):
self.current_row_new = self.main_self.ui_edit_radio_stations_window.stations_list.currentRow()
if(self.current_row_new!=self.current_row):
#ask for saving
box = QMessageBox()
box.setIcon(QMessageBox.Question)
box.setWindowTitle('Αποθήκευση αλλαγών')
box.setText('Θέλετε να αποθηκεύσετε τις αλλαγές σας;')
box.setStandardButtons(QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
buttonY = box.button(QMessageBox.Yes)
buttonY.setText('Ναι')
buttonN = box.button(QMessageBox.No)
buttonN.setText('Οχι')
buttonC = box.button(QMessageBox.Cancel)
buttonC.setText('Ακύρωση')
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/menu_window_icons/media/images/save.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
box.setWindowIcon(icon)
box.exec_()
if box.clickedButton() == buttonY:
self.save()
self.current_row = self.current_row_new
self.main_self.ui_edit_radio_stations_window.stations_list.setCurrentRow(self.current_row)
self.show_radio_connection_details()
elif box.clickedButton() == buttonN:
self.current_row = self.current_row_new
self.main_self.ui_edit_radio_stations_window.stations_list.setCurrentRow(self.current_row)
self.show_radio_connection_details()
elif box.clickedButton() == buttonC:
self.previous_item_timer=QTimer()
self.previous_item_timer.timeout.connect(self.return_to_previous_list_item)
self.previous_item_timer.setSingleShot(True)
self.previous_item_timer.start(100)
def return_to_previous_list_item(self):
self.main_self.ui_edit_radio_stations_window.stations_list.setCurrentRow(self.current_row)
self.current_row = self.main_self.ui_edit_radio_stations_window.stations_list.currentRow()
第一种方法被调用:使用这个命令:
self.main_self.ui_edit_radio_stations_window.stations_list.currentRowChanged.connect(lambda current_row:self.selected_radio_connection_changed(current_row))
其中stations_list 是一个QListWidget。
每次当前 qlist-item 改变时,就会打开一个 QMessageBox 提示。
使用前两个按钮,一切似乎都很好。但是当第三个被点击时,我想回滚到前一个 qlist-item。
问题是:为什么这个操作需要 QTimer?
我想在调用 selected_radio_connection_changed 方法之后有一个 event.accept() 。
解决方案
问题来自您试图在当前索引更改中“覆盖”当前索引的事实。您应该对此更加小心,因为这样的交互可能会导致递归问题。
要始终考虑的重要一点是模型视图的当前索引并不总是与选择匹配。
设置当前索引应该在“当前更改”结束时发生,因此您可以安全地使用 QTimer或使用视图的选择模型selectionChanged
信号。
不幸的是,您没有提供足够清晰的MRE,因此很难为您提供更具体的解决方案,这取决于您的需求以及您如何实施整个过程。
推荐阅读
- java - 是否可以从文本文件中读取文件结构并使用 JAVA 跟踪父节点和子节点?
- azure - Azure 数据工厂:源数据集具有用户定义的表类型的存储过程
- json - 是否可以动态过滤 JSON 对象,以便使用 Angular 仅显示与过滤器文本匹配的字段?
- ssl - IIS 10 中缺少 SSL 证书下拉列表
- asp.net - 响应标头 Content-Type 根据我用来发送请求的程序而改变
- javascript - Firebase 在尝试上传文件时给出“ERROR error [object Object]”
- java - 嵌入式 tomcat 中的 JAX-RS (Jersey) webapp 在 IDE 中工作,但不能作为 JAR
- r - 当R中的列中的值相似时迭代一个值
- c - 在c中反转字符串,使用strlen函数的问题
- routing - 有没有办法让凤凰插头只用于一条路线?