在 PyQt 中获取 ListWidget 项文本和图像
Getting ListWidget Item Text and Image in PyQt
我有一个 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())
由于 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_()
我有一个 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())
由于 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_()