Python PyQt5中的QListWidget-单击向上按钮时在列表中向上移动项目

Python PyQt5中的QListWidget-单击向上按钮时在列表中向上移动项目,python,pyqt5,repaint,qlistwidget,Python,Pyqt5,Repaint,Qlistwidget,我在pyQt5应用程序中有QListWidget 该列表包含一个项目列表 我通过单击列表中的项目来选择特定项目。 此后,我有一个QPushButton&Up,它应该在列表中向上移动项目,每次单击一次,直到它成为列表中的顶部项目。 当我单击按钮时,伪代码如下所示 get the row of the currently selected item if it is greater than row 1 then delete the item re i

我在pyQt5应用程序中有QListWidget
该列表包含一个项目列表
我通过单击列表中的项目来选择特定项目。
此后,我有一个QPushButton&Up,它应该在列表中向上移动项目,每次单击一次,直到它成为列表中的顶部项目。
当我单击按钮时,伪代码如下所示

get the row of the currently selected item
     if it is greater than row 1 then
         delete the item
         re insert the item at - (row - 1)
         set the newly inserted item as the current item
         **This should make the newly inserted item as the current 
           selected item with a blue band indicating selection**
但是,在我不调用列表小部件的重绘方法之前,这不会发生。

self.\uuuuu水果列表
是QListWidget
self.\uuu按钮\u up
是QPushButton

插槽连接定义如下:

self.__button_up.clicked.connect(self.__up)
@pyqtSlot()
    def __up(self):
        """Move the item up by one row."""
        row = self.__fruit_list.currentRow()
        if row >= 1:
            # Remove the currently selected item.
            item = self.__fruit_list.takeItem(row)
            self.__fruit_list.insertItem(row - 1, item)
            self.__fruit_list.setCurrentItem(item)
            self.__fruit_list.repaint()
插槽功能定义如下:

self.__button_up.clicked.connect(self.__up)
@pyqtSlot()
    def __up(self):
        """Move the item up by one row."""
        row = self.__fruit_list.currentRow()
        if row >= 1:
            # Remove the currently selected item.
            item = self.__fruit_list.takeItem(row)
            self.__fruit_list.insertItem(row - 1, item)
            self.__fruit_list.setCurrentItem(item)
            self.__fruit_list.repaint()
为什么我必须调用repaint方法,为什么列表不能自动刷新
有没有一种不用重新绘制()就可以完成的方法,或者我的方法有什么问题
如有任何意见,我们将不胜感激

MRE最简方案列表

"""Demonstrates QListWidget and adding and modifying values using a dialog."""
# Importing the standard python libraries.
import sys

# Importing the PyQt5 libraries.
from PyQt5.QtWidgets import QApplication, QAbstractItemView, QMessageBox
from PyQt5.QtWidgets import QMainWindow, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtWidgets import QListWidget, QPushButton
from PyQt5.QtCore import pyqtSlot

class FruitListApp(QMainWindow):
    """Fruit List Management Application class."""

    def __init__(self, parent=None):
        """Initialize the Fruit List Application."""
        super().__init__(parent)

        # Setup the widgets.
        self.__fruit_list = QListWidget()
        self.__fruit_list.setSelectionMode(QAbstractItemView.SingleSelection)

        self.__button_up = QPushButton("&Up")

        # Layout the widgets.
        button_box = QWidget()
        button_box_layout = QVBoxLayout()
        button_box_layout.addWidget(self.__button_up)

        button_box.setLayout(button_box_layout)

        central_widget = QWidget()
        layout = QHBoxLayout()
        layout.addWidget(self.__fruit_list)
        layout.addWidget(button_box)

        central_widget.setLayout(layout)

        # Setup the main application window.
        self.setCentralWidget(central_widget)
        self.setWindowTitle("Edit Fruit List")

        # Setup signals and slots.
        self.__button_up.clicked.connect(self.__up)

    def add_items(self, item_list):
        """Add items provided as a list to the fruit list."""
        # Item is a list.
        if isinstance(item_list, list):
            if len(item_list) > 0:
                # Add items to the list after determining the previous count
                # of items in the list.
                previous_count = self.__fruit_list.count()
                self.__fruit_list.addItems(item_list)
                # Set the first item added from a list as the current
                # selection.
                self.__fruit_list.setCurrentRow(previous_count)
            else:
                QMessageBox.warning(self, "Error", "The fruit names list "
                                               "provided is empty.")
                return
        else:
            QMessageBox.warning(self, "Error", "The fruit names are not "
                                               "provided as a list.")
            return

    @pyqtSlot()
    def __up(self):
        """Move the item up by one row."""
        row = self.__fruit_list.currentRow()
        if row >= 1:
            # Remove the currently selected item.
            item = self.__fruit_list.takeItem(row)
            self.__fruit_list.insertItem(row - 1, item)
            self.__fruit_list.setCurrentItem(item)
            self.__fruit_list.repaint()

if __name__ == "__main__":

    def main():
        """Main method to test the application."""
        # The fruit list.
        fruit = ["Banana", "Apple", "Elderberry", "Clementine", "Fig", "Guava",
                 "Mango", "Honeydew Melon", "Date", "Watermelon", "Tangerine",
                 "Ugli Fruit", "Juniperberry", "Kiwi", "Lemon", "Nectarine",
                 "Plum", "Raspberry", "Strawberry", "Orange"]

        # Instantiate the application.
        app = QApplication(sys.argv)

        # Instantiate the Fruit List Main Window.
        main_window = FruitListApp()
        # Add the fruit list.
        main_window.add_items(fruit)

        # Display the main application window.
        main_window.show()

        # Start the event loop.
        app.exec_()

    # Start the application.
    main()
**注释掉以下行以重现问题。**
self.\uuuu fruit\u list.repaint()

提供一个我已更新的帖子。谢谢。:)这不是一个MRE,我想要一个可以复制粘贴和执行代码的代码,显然你的代码不是。将此代码复制到一个.pyw文件中,并使用python3运行。再次感谢。你能给我一个错误的图像吗?除了这个,你使用的是哪个版本的PyQt5和操作系统?