微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

QListWidget:currentRowChanged 回滚

如何解决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,因此很难为您提供更具体的解决方案,这取决于您的需求以及您如何实施整个程序。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。