代码之家  ›  专栏  ›  技术社区  ›  ChronoVortex

引用pyqt5 listWidget中的各个子窗口小部件

  •  0
  • ChronoVortex  · 技术社区  · 2 年前

    我有一堆小部件,我想添加到 QListWidget 。到目前为止,代码似乎正在工作,如果不是因为我如何用笨拙的for循环构建它,看起来小部件既不能被自己引用,也不能从其他小部件引用:

    for i in self.csv:
                    self.constructListItem(i,
                                           type,
                                           {
                                           'Option1':[False],
                                           'Option2':[True,'desc1'],
                                           'Option3':[True,'desc2'],
                                           'Option4':[False],
                                            })
    

    哪里 self.constructListItem 看起来是这样的:

    def constructListItem(self, columnName, defaultOption, listOptions):
            listFeatureWidget = QtWidgets.QListWidgetItem()
            featureWidget = QtWidgets.QWidget()
            #widget details
            featureWidget.setStyleSheet('background-color:transparent')
            featureWidget.setGeometry(QtCore.QRect(50, 210, 901, 91))
            featureWidget.setObjectName("featureWidget")
            verticalLayout = QtWidgets.QVBoxLayout(featureWidget)
            verticalLayout.setContentsMargins(0, 0, 0, 0)
            verticalLayout.setSpacing(0)
            verticalLayout.setObjectName("verticalLayout")
            feature = QtWidgets.QWidget(featureWidget)
            feature.setObjectName("feature")
            horizontalLayout_8 = QtWidgets.QHBoxLayout(feature)
            horizontalLayout_8.setObjectName("horizontalLayout_8")
            featureLabel = QtWidgets.QLabel(feature)
            featureLabel.setObjectName("featureLabel")
            featureLabel.setText(columnName)
            horizontalLayout_8.addWidget(featureLabel)
            comboBox = QtWidgets.QComboBox(feature)
            comboBox.setStyleSheet('background-color:white')
            comboBox.setObjectName("comboBox")
            comboBox.currentTextChanged.connect(updateSelf)
            #add items from dict into comboBox
            for i in listOptions.keys():
                comboBox.addItem(i)
            comboBox.setCurrentIndex(defaultOption)
            horizontalLayout_8.addWidget(comboBox)
            verticalLayout.addWidget(feature)
            parameterWidget = QtWidgets.QWidget(featureWidget)
            parameterWidget.setObjectName("parameterWidget")
            horizontalLayout = QtWidgets.QHBoxLayout(parameterWidget)
            horizontalLayout.setObjectName("horizontalLayout")
            parameterLabel = QtWidgets.QLabel(parameterWidget)
            parameterLabel.setObjectName("parameterLabel")
            horizontalLayout.addWidget(parameterLabel)
            spinBox = QtWidgets.QSpinBox(parameterWidget)
            spinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.UpDownArrows)
            spinBox.setMinimum(1)
            spinBox.setObjectName("spinBox")
            horizontalLayout.addWidget(spinBox)
            spacerItem = QtWidgets.QSpacerItem(653, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
            horizontalLayout.addItem(spacerItem)
            verticalLayout.addWidget(parameterWidget)
            listFeatureWidget.setSizeHint(featureWidget.sizeHint())
            self.featureList.addItem(listFeatureWidget)
            self.featureList.setItemWidget(listFeatureWidget,featureWidget)
    

    所需的行为是,每当小部件的comboBox值发生变化时,它都会根据传递的字典和值collapse/expand parameterWidget进行检查,并设置其中一个标签的文本。那么我该怎么写呢 updateSelf 函数,以便它可以引用正确的小部件?

    编辑: 经过一番研究,我意识到这需要上课,所以我想出了这个主意:

    class featureItemInstance:
            def __init__(self, master, columnName, defaultOption, listOptions) -> None:
                self.listOptions = listOptions
                self.listFeatureWidget = QtWidgets.QListWidgetItem()
                self.featureWidget = QtWidgets.QWidget()
                self.featureWidget.setStyleSheet('background-color:transparent')
                self.featureWidget.setGeometry(QtCore.QRect(50, 210, 901, 91))
                self.featureWidget.setObjectName("featureWidget")
                self.verticalLayout = QtWidgets.QVBoxLayout(self.featureWidget)
                self.verticalLayout.setContentsMargins(0, 0, 0, 0)
                self.verticalLayout.setSpacing(0)
                self.verticalLayout.setObjectName("verticalLayout")
                self.feature = QtWidgets.QWidget(self.featureWidget)
                self.feature.setObjectName("feature")
                self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.feature)
                self.horizontalLayout_8.setObjectName("horizontalLayout_8")
                self.featureLabel = QtWidgets.QLabel(self.feature)
                self.featureLabel.setObjectName("featureLabel")
                self.featureLabel.setText(columnName)
                self.horizontalLayout_8.addWidget(self.featureLabel)
                self.comboBox = QtWidgets.QComboBox(self.feature)
                self.comboBox.setStyleSheet('background-color:white')
                self.comboBox.setObjectName("comboBox")
                self.comboBox.currentTextChanged.connect(self.updateSelf)
                for i in listOptions.keys():
                    self.comboBox.addItem(i)
                self.comboBox.setCurrentIndex(defaultOption)
                self.horizontalLayout_8.addWidget(self.comboBox)
                self.verticalLayout.addWidget(self.feature)
                self.parameterWidget = QtWidgets.QWidget(self.featureWidget)
                self.parameterWidget.setObjectName("parameterWidget")
                self.horizontalLayout = QtWidgets.QHBoxLayout(self.parameterWidget)
                self.horizontalLayout.setObjectName("horizontalLayout")
                self.parameterLabel = QtWidgets.QLabel(self.parameterWidget)
                self.parameterLabel.setObjectName("parameterLabel")
                self.horizontalLayout.addWidget(self.parameterLabel)
                self.spinBox = QtWidgets.QSpinBox(self.parameterWidget)
                self.spinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.UpDownArrows)
                self.spinBox.setMinimum(1)
                self.spinBox.setObjectName("spinBox")
                self.horizontalLayout.addWidget(self.spinBox)
                self.spacerItem = QtWidgets.QSpacerItem(653, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
                self.horizontalLayout.addItem(self.spacerItem)
                self.verticalLayout.addWidget(self.parameterWidget)
                self.listFeatureWidget.setSizeHint(self.featureWidget.sizeHint())
                master.featureList.addItem(self.listFeatureWidget)
                master.featureList.setItemWidget(self.listFeatureWidget,self.featureWidget)
                self.updateSelf()
            
            def updateSelf(self):
                expand = self.listOptions[self.comboBox.currentText()]
                if expand[0]:
                    self.parameterWidget.show()
                    self.parameterLabel.setText(expand[1])
                else:
                    self.parameterWidget.hide()
    

    但由于某种原因 updateSelf() 上面写着 'featureItemInstance' object has no attribute 'parameterWidget' 尽管在 __init__ 不知道怎么了。

    0 回复  |  直到 2 年前