【问题标题】:PyQt QListWidget doesn't show custom widgets as itemsPyQt QListWidget 不会将自定义小部件显示为项目
【发布时间】:2018-06-20 15:14:48
【问题描述】:

我想将 NodeLinkItemWidgetUI 表单(自定义小部件)作为项目添加到我的主应用程序中的 ListWidget。问题是,正确小部件大小的项目作为项目添加到列表中,但项目内容为空。 这是来自NodeLinkItemWidgetUI 的项目自定义小部件的代码:

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Form(QtWidgets.QWidget):
    def __init__(self, nodename, nodeclass, parent=None):
        super(Ui_Form,self).__init__(parent)
        self.comboBox_type = QtWidgets.QComboBox()
        self.comboBox_type.setGeometry(QtCore.QRect(10, 100, 51, 25))
        self.comboBox_type.setCurrentText("connector")
        self.comboBox_type.setObjectName("comboBox_type")
        self.comboBox_type.addItem("")
        self.comboBox_type.setItemText(0, "connector")
        self.comboBox_type.addItem("")
        self.comboBox_type.setItemText(1, "dot")
        self.comboBox_type.addItem("")
        self.comboBox_type.setItemText(2, "link")
        self.checkBox_hide = QtWidgets.QCheckBox()
        self.checkBox_hide.setGeometry(QtCore.QRect(70, 100, 51, 23))
        self.checkBox_hide.setText("hide")
        self.checkBox_hide.setIconSize(QtCore.QSize(16, 16))
        self.checkBox_hide.setTristate(False)
        self.checkBox_hide.setObjectName("checkBox_hide")
        self.pushButton = QtWidgets.QPushButton()
        self.pushButton.setGeometry(QtCore.QRect(10, 130, 111, 25))
        self.pushButton.setText("create")
        self.pushButton.setObjectName("pushButton")
        self.label_icon = QtWidgets.QLabel()
        self.label_icon.setGeometry(QtCore.QRect(15, 10, 100, 80))
        self.label_icon.setText("")
        self.label_icon.setObjectName("label_icon")
        self.label_nodename = QtWidgets.QLabel()
        self.label_nodename.setGeometry(QtCore.QRect(15, 10, 100, 80))
        font = QtGui.QFont()
        font.setWeight(75)
        font.setBold(True)
        self.label_nodename.setFont(font)
        self.label_nodename.setText(nodename)
        self.label_nodename.setAlignment(QtCore.Qt.AlignCenter)
        self.label_nodename.setObjectName("label_nodename")
        QtCore.QMetaObject.connectSlotsByName(self)

这里是我的主应用程序中的代码,它们作为项目添加到我的列表小部件中:

class Connecthor(QtWidgets.QMainWindow, Ui_MainWindow):
#constructor
def __init__(self, parent=None):
    super(Connecthor, self).__init__(parent)
    self.setupUi(self)

    self.setFixedSize(self.size())

    #ADDING TEST WIDGETS
    for i in range(5):
        self.createNewLink("test","")


def createNewLink(self, nodename, nodeclass):
    item = QtWidgets.QListWidgetItem(self.listWidget_links)
    item_widget = NodeLinkItemWidgetUI.Ui_Form(nodename=nodename, nodeclass=nodeclass)

    #size hint doesn't return the correnct widget size so I did set it manually
    # print item_widget.sizeHint()
    # item.setSizeHint(item_widget.sizeHint())

    item.setSizeHint(QtCore.QSize(130, 160))
    self.listWidget_links.addItem(item)
    self.listWidget_links.setItemWidget(item, item_widget)

【问题讨论】:

  • 显示完整的 Ui_Form 类。
  • 好的,我更新了帖子!

标签: qt pyqt5 qlistwidget qtwidgets


【解决方案1】:

如果这些是你的孩子,一个小部件只显示里面的其他小部件,在你的self.comboBox_typeself.checkBox_hideself.pushButtonself.label_icon 没有父亲,所以他们不会出现,解决方案是传递一个父,在本例中为self

class Ui_Form(QtWidgets.QWidget):
    def __init__(self, nodename, nodeclass, parent=None):
        super(Ui_Form,self).__init__(parent)
        self.comboBox_type = QtWidgets.QComboBox(self) # <---
        ...
        self.checkBox_hide = QtWidgets.QCheckBox(self)
        ...
        self.pushButton = QtWidgets.QPushButton(self) # <---
        ...
        self.label_icon = QtWidgets.QLabel(self) # <---
        ...

【讨论】:

  • 好的,我必须承认我还没有理解这个答案,因为到目前为止我还没有理解父母的东西。但这确实是正确答案!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 2018-04-20
  • 2015-11-19
  • 2010-10-31
  • 1970-01-01
  • 2017-05-26
相关资源
最近更新 更多