在pyqt5 ListWidget中引用单个儿童小部件
我有很多小部件要添加到qlistwidget
中。到目前为止,该代码似乎正在起作用,如果不是因为它看起来不像小部件本身或其他小部件可以引用小部件,因为我是如何用笨拙的循环构造的:
for i in self.csv:
self.constructListItem(i,
type,
{
'Option1':[False],
'Option2':[True,'desc1'],
'Option3':[True,'desc2'],
'Option4':[False],
})
where code> 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值更改时,它都会根据传递的字典进行检查,并根据值为collaspe/explang afffent commeterwidget并设置其中一个标签的文字。那么,如何编写此更新
函数,以便它可以引用正确的小部件?
编辑: 稍后,我意识到它需要上课,所以我想到了:
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()
它说'farterainiteMinstance'对象没有属性'parameterWidget '
尽管在__ Init __
中被声明,但不知道怎么了。
I have a bunch of widgets that I want to add into a QListWidget
. So far the code seems to be working, if not for the issue that it does not look like the widgets can be referenced either by itself or from other widgets due to how I constructed it with a clumsy for loop:
for i in self.csv:
self.constructListItem(i,
type,
{
'Option1':[False],
'Option2':[True,'desc1'],
'Option3':[True,'desc2'],
'Option4':[False],
})
where self.constructListItem
looks like this:
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)
The desired behaviour is that everytime a widget's comboBox value changes, it will check against the dictionary passed and based on what the value is collaspe/expand parameterWidget and set the text of one of it's labels. So how do I write this updateSelf
function so that it can reference the correct widget?
Edit:
A bit of researching later I realised that it needs to be class so I came up with this:
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()
but for some reason in updateSelf()
it says that 'featureItemInstance' object has no attribute 'parameterWidget'
despite being declared within the __init__
, no idea what's wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论