在 PyQt 中获取 ListWidget 项文本和图像
Posted
技术标签:
【中文标题】在 PyQt 中获取 ListWidget 项文本和图像【英文标题】:Getting ListWidget Item Text and Image in PyQt 【发布时间】:2016-01-21 20:32:55 【问题描述】:我有一个 QListwidget
。我添加了一个显示图像的自定义项目。我想要做的是单击此小部件中的项目时,打印选定的标签文本并获取标签的图像。但它什么也不打印并给出错误:AttributeError: 'QListWidgetItem' object has no attribute 'pixmap'
。那么问题是什么?
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())
【问题讨论】:
【参考方案1】:由于 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_()
【讨论】:
以上是关于在 PyQt 中获取 ListWidget 项文本和图像的主要内容,如果未能解决你的问题,请参考以下文章
Python:如何在 PyQt 中查询 QListWidget 中的多个选定项
PyQt4:将项目从一个 QListWidget 移动到另一个