使用自定义小部件填充

使用自定义小部件填充

本文介绍了使用自定义小部件填充 QListWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含许多这些项目的 QListView 或 QListWidget.我看过制作委托并使用 setItemWidget 来设置小部件.我在这两个方面都遇到了困难.

I would like to create a QListView or QListWidget that contains many of these items. I have looked at making delegates and also using setItemWidget to set the widget. I am having difficulties with both.

下面是在 QT Designer 中创建的,然后我有一个 UIC 文件,我将它加载到带有 uic.loadUi 的子类 QWidget 中,没有任何崩溃,但 listWidgetItems 为空.

This below was created in QT Designer, I then have a UIC file that I load in a subclassed QWidget with uic.loadUi, nothing is crashing, but the listWidgetItems are empty.

任何帮助将不胜感激.我将在下面对此进行迭代,因此我希望能够动态加载 UIC.

Any help would be appreciated. I will be iterating on this below, so I would like to be able to load a UIC dynamically.

可能有数以千计的动画,所以如果可能的话,我想保持它的性能.

There could be thousands of animations, so I would like to keep it performant if possible.

推荐答案

所以,我不能用委托来做,但这里是通过加载从 QtDesigner 生成的自定义小部件加载到 listView 中的代码使用 setItemWidget:

So, I couldn't do it with a delegate, but here's the code for doing it with loading a custom widget generated from QtDesigner that is loaded into the listView with setItemWidget:

import os, sys
import xml.etree.ElementTree as et
from PyQt4 import QtGui, QtCore, uic

class uiControlTest(QtGui.QMainWindow):
    def __init__(self):
        super(uiControlTest, self).__init__()
        self.ui = uic.loadUi('uiControlTest.ui')
        self.ui.show()

        for i in range(0,300):
            wid = animItemWidget()
            wid.label_2.setText('Last edited by chrise @2014.06.21:23:17')
            wid.label.setText('Animation ' + str(i) + '       ')

            wid2 = QtGui.QListWidgetItem()
            wid2.setSizeHint(QtCore.QSize(100, 40))
            self.ui.list.addItem(wid2)
            self.ui.list.setItemWidget(wid2, wid)

        def awesomeButtonPressed():
            print 'awesome!'

class animItemWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(animItemWidget, self).__init__()
        self.horizontalLayout_4 = QtGui.QHBoxLayout(self)
        self.horizontalLayout_4.setSpacing(2)
        self.horizontalLayout_4.setMargin(3)
        self.horizontalLayout_4.setObjectName(QtCore.QString.fromUtf8("horizontalLayout_4"))
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setObjectName(QtCore.QString.fromUtf8("verticalLayout_2"))
        self.label = QtGui.QLabel(self)
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setWeight(75)
        font.setBold(True)
        self.label.setFont(font)
        self.label.setObjectName(QtCore.QString.fromUtf8("label"))
        self.verticalLayout_2.addWidget(self.label)
        self.pixMap02 = QtGui.QLabel(self)
        self.pixMap02.setText(QtCore.QString.fromUtf8(""))
        self.pixMap02.setObjectName(QtCore.QString.fromUtf8("pixMap02"))
        self.verticalLayout_2.addWidget(self.pixMap02)
        self.horizontalLayout_4.addLayout(self.verticalLayout_2)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(QtCore.QString.fromUtf8("verticalLayout"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setSpacing(2)
        self.horizontalLayout.setObjectName(QtCore.QString.fromUtf8("horizontalLayout"))
        self.pixMap01 = QtGui.QLabel(self)
        self.pixMap01.setText(QtCore.QString.fromUtf8(""))
        self.pixMap01.setObjectName(QtCore.QString.fromUtf8("pixMap01"))
        self.horizontalLayout.addWidget(self.pixMap01)
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.button02 = QtGui.QPushButton(self)
        self.button02.setMaximumSize(QtCore.QSize(24, 24))
        self.button02.setText(QtCore.QString.fromUtf8(""))
        self.button02.setObjectName(QtCore.QString.fromUtf8("button02"))
        self.horizontalLayout.addWidget(self.button02)
        self.button01 = QtGui.QPushButton(self)
        self.button01.setMaximumSize(QtCore.QSize(24, 24))
        self.button01.setText(QtCore.QString.fromUtf8(""))
        self.button01.setObjectName(QtCore.QString.fromUtf8("button01"))
        self.horizontalLayout.addWidget(self.button01)
        self.button04 = QtGui.QPushButton(self)
        self.button04.setMaximumSize(QtCore.QSize(24, 24))
        self.button04.setText(QtCore.QString.fromUtf8(""))
        self.button04.setObjectName(QtCore.QString.fromUtf8("button04"))
        self.horizontalLayout.addWidget(self.button04)
        self.button03 = QtGui.QPushButton(self)
        self.button03.setMaximumSize(QtCore.QSize(24, 24))
        self.button03.setText(QtCore.QString.fromUtf8(""))
        self.button03.setObjectName(QtCore.QString.fromUtf8("button03"))
        self.horizontalLayout.addWidget(self.button03)
        self.button05 = QtGui.QPushButton(self)
        self.button05.setMaximumSize(QtCore.QSize(24, 24))
        self.button05.setText(QtCore.QString.fromUtf8(""))
        self.button05.setObjectName(QtCore.QString.fromUtf8("button05"))
        self.horizontalLayout.addWidget(self.button05)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName(QtCore.QString.fromUtf8("horizontalLayout_3"))
        self.label_2 = QtGui.QLabel(self)
        self.label_2.setObjectName(QtCore.QString.fromUtf8("label_2"))
        self.horizontalLayout_3.addWidget(self.label_2)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4.addLayout(self.verticalLayout)

        self.connect(self.button02, QtCore.SIGNAL("clicked()"), self.awesome)

    def awesome(self):
        print self.label.text() + ' is awesome!'


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = uiControlTest()
    sys.exit(app.exec_())

这篇关于使用自定义小部件填充 QListWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:42