在PyQt中获取ListWidget项目文本和图像

问题描述:

我有一个QListwidget。我添加了一个显示图像的自定义项目。我想要做的是点击此小部件中的项目时,打印选定的标签文本并获取标签的图像。但它不打印任何内容并给出错误:AttributeError: 'QListWidgetItem' object has no attribute 'pixmap'。那么问题是什么?在PyQt中获取ListWidget项目文本和图像

class MyList(QtGui.QListWidget): 
    def __init__(self): 
     QtGui.QListWidget.__init__(self) 
     directory = QtGui.QFileDialog.getOpenFileNames(self, 'Open file', 
                 'c:\\Users\\Public\\Pictures\\Sample Pictures',"Image files (*.jpg *.gif)") 
     for i in range(len(directory)): 
      images = QtGui.QImage(directory[i]) 
      pixmap = QtGui.QPixmap.fromImage(images) 
      label = QtGui.QLabel(str(i)) 
      label.setPixmap(pixmap.scaled(QtCore.QSize(150,100))) 
      item = QtGui.QListWidgetItem(label.text()) 
      item.setSizeHint(QtCore.QSize(200,110)) 
      self.addItem(item) 
      self.setItemWidget(item,label) 
     self.setIconSize(QtCore.QSize(150,100)) 
     self.setSelectionMode(1)   # 1 = SingleSelection, 2 = MultiSelection, not necessary, default mode is singleSelection 
     self.setGeometry(200,200,300,500) 

     self.currentItemChanged.connect(self.findSel) 

    def findSel(self, current, previous): 
     print(current.text()) 
     self.labelBigImageDisplay(current.pixmap()) 

由于QLabel不支持文本和图像同一时间,你可能需要考虑实现自己的小部件,或者您可能需要使用像QPushButton等。而回答你原来的问题,你需要问itemWidget获取数据。

def findSel(self, current): 
    currentItem = self.itemWidget(current) 
    pixmap = currentItem.pixmap() 
    print pixmap 

实现自定义窗口小部件

这是你的代码小例子

mport sys 
from PyQt4 import QtCore, QtGui 

class MyCustomWid(QtGui.QWidget): 
    def __init__(self, label, imagePath, parent=None): 
     super(MyCustomWid, self).__init__(parent) 
     horizontalLayout = QtGui.QHBoxLayout() 
     self.imagePath = imagePath 
     self.captLbl = label 
     self.captLbl = QtGui.QLabel(self.captLbl) 
     horizontalLayout.addWidget(self.captLbl) 
     self.imageLbl = QtGui.QLabel() 
     pixmap = QtGui.QPixmap.fromImage(QtGui.QImage(self.imagePath)) 
     self.imageLbl.setPixmap(pixmap.scaled(QtCore.QSize(150,100))) 
     horizontalLayout.addWidget(self.imageLbl) 
     self.setLayout(horizontalLayout) 

    def getPixmap(self): 
     return self.imageLbl.pixmap() 

    def getImagePath(self): 
     return self.imagePath 

    def getText(self): 
     return self.captLbl.text() 

class MyList(QtGui.QListWidget): 
    def __init__(self): 
     QtGui.QListWidget.__init__(self) 
     imagePath = "/usr/bla/some/foo.png" 
     label = MyCustomWid("Blaa", imagePath) 
     item = QtGui.QListWidgetItem() 
     item.setSizeHint(QtCore.QSize(200,110)) 
     self.addItem(item) 
     self.setItemWidget(item,label) 
     self.setIconSize(QtCore.QSize(150,100)) 
     self.setSelectionMode(1)   # 1 = SingleSelection, 2 = MultiSelection, not necessary, default mode is singleSelection 
     self.setGeometry(200,200,300,500) 
     self.itemClicked.connect(self.findSel) 

    def findSel(self, current): 
     currentItem = self.itemWidget(current) 
     pixmap = currentItem.getPixmap() 
     imagePath = currentItem.getImagePath() 
     lblTxt = currentItem.getText() 
     print pixmap, imagePath, lblTxt 
     # self.labelBigImageDisplay(current.pixmap()) 

class MyWindow(QtGui.QDialog): 
    def __init__(self): 
     super(MyWindow, self).__init__() 
     layout = QtGui.QVBoxLayout() 
     textLbl = MyList() 
     layout.addWidget(textLbl) 
     self.setLayout(layout) 

if __name__ == '__main__': 
    app=QtGui.QApplication(sys.argv) 
    new=MyWindow() 
    new.show() 
    app.exec_()