想要实现单击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