0

我正在尝试设计一个项目列表,其中每个项目都是一个自定义小部件(但不一定,我想在每个项目中显示一个图像和三个文本)。在 PyQt 中,函数addItem可用于将项目添加到以QListWidgetItem为参数的列表中。

因此,我试图将我自定义的小部件转换为 QListWidetItem ,但没有成功。我尝试了以下代码块:

listWidget = QtWidgets.QListWidget(self.centralwidget)
item = QListWidgetItem(self.listWidget)
custom_item = CustomWidget()
listWidget.addItem(item)
listWidget.setItemWidget(item, item_widget)

该列表显示一个空列表,这是意料之中的,因为我从不使用上面的代码夸大 custom_item。如何在 PyQT 中解决这个问题?

我不确定,但自定义小部件可能是相关的:

class CustomWidget(QWidget):
    def __init__(self, user):
        QWidget.__init__(self)
        self.user = user
        self.horizontalLayoutWidget = QtWidgets.QWidget()
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 211, 41))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.UserImage = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.UserImage.setText("This is an Image")
        self.UserImage.setObjectName("UserImage")
        self.horizontalLayout.addWidget(self.UserImage)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.UserName = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.UserName.setText("My name is jeff")
        self.UserName.setObjectName("UserName")
        self.verticalLayout.addWidget(self.UserName)
        self.UserStatus = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.UserStatus.setText("I am available")
        self.UserStatus.setObjectName("UserStatus")
        self.verticalLayout.addWidget(self.UserStatus)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.horizontalLayout.setStretch(0, 1)
        self.horizontalLayout.setStretch(1, 3)
4

1 回答 1

1

我从您的代码中注意到,该小部件未正确构建,self.horizontalLayout是主小部件,但它没有父小部件,因此永远不会被绘制,此外您可以避免使用该小部件。另一方面,您必须设置项目的大小setSizeHint()

from PyQt5 import QtWidgets, QtCore

class CustomWidget(QtWidgets.QWidget):
    def __init__(self, user, *args, **kwargs):
        QtWidgets.QWidget.__init__(self, *args, **kwargs)
        self.user = user
        self.setGeometry(QtCore.QRect(0, 0, 211, 41))
        self.horizontalLayout = QtWidgets.QHBoxLayout(self)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.UserImage = QtWidgets.QLabel(self)
        self.UserImage.setText("This is an Image")
        self.UserImage.setObjectName("UserImage")
        self.horizontalLayout.addWidget(self.UserImage)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.UserName = QtWidgets.QLabel(self)
        self.UserName.setText("My name is jeff")
        self.UserName.setObjectName("UserName")
        self.verticalLayout.addWidget(self.UserName)
        self.UserStatus = QtWidgets.QLabel(self)
        self.UserStatus.setText("I am available")
        self.UserStatus.setObjectName("UserStatus")
        self.verticalLayout.addWidget(self.UserStatus)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.horizontalLayout.setStretch(0, 1)
        self.horizontalLayout.setStretch(1, 3)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    listWidget = QtWidgets.QListWidget()
    item = QtWidgets.QListWidgetItem(listWidget)
    item_widget = CustomWidget("user")
    listWidget.addItem(item)
    listWidget.setItemWidget(item, item_widget)
    item.setSizeHint(item_widget.sizeHint())
    listWidget.show()
    sys.exit(app.exec_())

在此处输入图像描述

于 2017-11-24T13:15:00.933 回答