我正在尝试设计一个项目列表,每个项目都是自定义小部件(不一定,我想在每个项目中显示图像和三个文本)。 在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)
答案 0 :(得分: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_())