PyQt QListWidget 自定义项 [英] PyQt QListWidget custom items

查看:94
本文介绍了PyQt QListWidget 自定义项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个 QListWidgetItem,它下面有 1 个图像和 2 个标签/字符串,支持 css?

how can i create a QListWidgetItem that has 1 image and 2 labels/strings underneath, that have support for 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)

顺便说一句,我正在使用 PyQt

i'm using PyQt btw

推荐答案

如何创建具有 1 个图像和 2 个图像的 QListWidgetItem下面的标签/字符串支持 css?

how can i create a QListWidgetItem that has 1 image and 2 labels/strings underneath, that have support for css?

在这种情况下,你不能(它实际上有一个可以轻松添加图标的 API,但两个标签/字符串是不可能的).但是,您可以创建自己的自定义小部件并将其放入QtGui.QListWidget.

In this case, you can't (it actually has an API for adding icons easily, but two labels/strings is impossible). But, you can create your own custom widget and put it into QtGui.QListWidget.

  1. 创建您的自定义小部件.

  1. Create your custom widget.

在主应用程序中创建您的 QtGui.QListWidget.

Create your QtGui.QListWidget in the main application.

创建一个自定义小部件对象并在 QListWidgetItem 中通过 QListWidgetItem 设置项目在 QtGui.QListWidget 中,使用 QListWidget.setItemWidget (self, QListWidgetItem item, QWidget widget) 方法.

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.

这是一个解释我的解决方案的例子:

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_())

注意:我有图像文件 icon.png,大小为 48 x 48 像素.

Note: I have image file icon.png, size 48 x 48 pixel.

QListWidget.setItemWidget

实验结果

这篇关于PyQt QListWidget 自定义项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆