I have a QListwidget
. I add a custom item that displaying images . What I want to do is when clicking an item in this widget , print selected labels text and get label's image. But it prints nothing and gives error: AttributeError: 'QListWidgetItem' object has no attribute 'pixmap'
. So what is the problem ?
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())
Since QLabel don't support text and image same time you may need to consider implementing your own widget or you may need to use something like QPushButton etc .. And answer for your original question you need to ask for the itemWidget to get the data.
def findSel(self, current):
currentItem = self.itemWidget(current)
pixmap = currentItem.pixmap()
print pixmap
Implementing custom widget
This is minimal example from your code
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_()