PyQt5中Qlistwidget的itemClicked信号问题,自定义槽函数后无法打开界面

想要实现单击Qlistwidget中的一行并实现相关操作,mainwindow类构造函数定义了如下代码:

self.fire_list.setSelectionMode(QAbstractItemView.SingleSelection)
self.fire_list.itemClicked.connect(self.show)

类下自定义槽函数:

# 槽函数
def show(self):
        item = self.fire_list.selectedItems()[0]
        filename = item.text() 
        folder_path = self.file_lineEdit.text()
        file_path = join(folder_path,filename)
        img = cv.imread(file_path,1)
        img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
        qimg = QImage(img.shape[1], img.shape[0], img.shape[1]*3, QImage.Format_RGB888)
        pixmap = QPixmap.fromImage(qimg)
        self.img_label.setPixmap(pixmap.scaled(self.Image_label.size(), Qt.KeepAspectRatio))

但是报了数组超出范围错误,不应等我点击item才触发show吗?错误如下:

Traceback (most recent call last):
  File "d:/code/python/paper_code/main_gui.py", line 134, in <module>
    main_win.show()
  File "d:/code/python/paper_code/main_gui.py", line 116, in show
    item = self.fire_list.selectedItems()[0]
IndexError: list index out of range

print(self.fire_list.selectedItems())看看这个数据结构,它应该是个空列表,因为你的提示是超出下标索引了

找到问题了,槽函数命名为了show,相当于重写了mainWindow的show()函数,导致主程序中主窗口显示时就要获取selectedItems,但那时候listWidget中为空。