Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QListWidget:currentRowChanged回滚_Python_Pyqt5_Qlistwidget - Fatal编程技术网

Python QListWidget:currentRowChanged回滚

Python QListWidget:currentRowChanged回滚,python,pyqt5,qlistwidget,Python,Pyqt5,Qlistwidget,第一个方法被调用:使用以下命令: 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

第一个方法被调用:使用以下命令:

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()
其中stations\u列表是一个QListWidget

每次更改当前qlist项时,都会打开QMessageBox提示符

使用前两个按钮,一切似乎都正常。 但是当第三个被单击时,我想回滚到前面的qlist项

问题是:为什么此操作需要QTimer


我假设在调用所选的\u radio\u connection\u changed方法之后有一个event.accept()。

问题是因为您试图在当前索引更改中“覆盖”当前索引。您应该更加小心,因为这样的交互可能会导致递归问题

始终要考虑的是,模型视图的当前索引并不总是与选择匹配。

设置当前索引应该发生在“当前更改”的末尾,因此您可以安全地使用QTimer或使用视图的选择模型信号


不幸的是,您没有提供足够清晰的解决方案,因此很难为您提供更具体的解决方案,这取决于您的需要以及您如何实施整个过程。

好的,我理解。谢谢你的回答。
self.main_self.ui_edit_radio_stations_window.stations_list.currentRowChanged.connect(lambda current_row:self.selected_radio_connection_changed(current_row))