[英]PyQt QListWidget custom items
how can i create a QListWidgetItem that has 1 image and 2 labels/strings underneath, that have support for css? 我如何创建一个QListWidgetItem,其下方具有1个图像和2个标签/字符串,并且支持CSS?
this is the last thing i have tried: 这是我尝试过的最后一件事:
class CustomListWidgetItem(QListWidgetItem, QLabel):
def __init__(self, parent=None):
QListWidgetItem.__init__(self, parent)
QLabel.__init__(self, parent)
i'm using PyQt btw 我正在使用PyQt btw
how can i create a QListWidgetItem that has 1 image and 2 labels/strings underneath, that have support for css?
我如何创建一个QListWidgetItem,其下方具有1个图像和2个标签/字符串,并且支持CSS?
In this case, you can't (it actually has an API for adding icons easily, but two labels/strings is impossible). 在这种情况下,您就不能这样做(它实际上具有一个可以轻松添加图标的API,但是两个标签/字符串是不可能的)。 But, you can create your own custom widget and put it into
QtGui.QListWidget
. 但是,您可以创建自己的自定义窗口小部件并将其放入
QtGui.QListWidget
。
Create your custom widget. 创建您的自定义窗口小部件。
Create your QtGui.QListWidget
in the main application. 在主应用程序中创建
QtGui.QListWidget
。
Create a custom widget object and set item in QListWidgetItem
by QListWidgetItem
is in QtGui.QListWidget
by using the QListWidget.setItemWidget (self, QListWidgetItem item, QWidget widget)
method. 创建在定制控件对象和设定项目
QListWidgetItem
通过QListWidgetItem
是在QtGui.QListWidget
通过使用QListWidget.setItemWidget (self, QListWidgetItem item, QWidget widget)
方法。
This is an example to explain my solution: 这是一个说明我的解决方案的示例:
import sys
from PyQt4 import QtGui
class QCustomQWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.textQVBoxLayout = QtGui.QVBoxLayout()
self.textUpQLabel = QtGui.QLabel()
self.textDownQLabel = QtGui.QLabel()
self.textQVBoxLayout.addWidget(self.textUpQLabel)
self.textQVBoxLayout.addWidget(self.textDownQLabel)
self.allQHBoxLayout = QtGui.QHBoxLayout()
self.iconQLabel = QtGui.QLabel()
self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
self.setLayout(self.allQHBoxLayout)
# setStyleSheet
self.textUpQLabel.setStyleSheet('''
color: rgb(0, 0, 255);
''')
self.textDownQLabel.setStyleSheet('''
color: rgb(255, 0, 0);
''')
def setTextUp (self, text):
self.textUpQLabel.setText(text)
def setTextDown (self, text):
self.textDownQLabel.setText(text)
def setIcon (self, imagePath):
self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath))
class exampleQMainWindow (QtGui.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
# Create QListWidget
self.myQListWidget = QtGui.QListWidget(self)
for index, name, icon in [
('No.1', 'Meyoko', 'icon.png'),
('No.2', 'Nyaruko', 'icon.png'),
('No.3', 'Louise', 'icon.png')]:
# Create QCustomQWidget
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.setTextUp(index)
myQCustomQWidget.setTextDown(name)
myQCustomQWidget.setIcon(icon)
# Create QListWidgetItem
myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget)
# Set size hint
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
# Add QListWidgetItem into QListWidget
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
self.setCentralWidget(self.myQListWidget)
app = QtGui.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
Note: I have image file icon.png
, size 48 x 48 pixel. 注意:我有图像文件
icon.png
,大小为48 x 48像素。
QListWidget.setItemWidget QListWidget.setItemWidget
Experimental result 实验结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.