我的项目包含具有多个 QGroupbox 的 Qdialog。每个 GroupBox 包含一些复选框。所有 groupbox 的复选框列表都是相同的。我没有太多声誉来加载图像:(
在这里,用户可以根据需要选择复选框并按下确定按钮。按下确定按钮后,我应该能够获得用户选中的复选框列表。
我在循环中创建复选框,代码如下:
def createGroupBox(self,livename,shotlist):
groupBox = QtGui.QGroupBox("Live-"+livename)
grpLayout = QtGui.QVBoxLayout()
i=0
while i != (len(shotlist)-2):
qChkBx_shot = QtGui.QCheckBox("Shot-"+shotlist[i], self)
qChkBx_shot.stateChanged.connect(lambda: self.groupcheckBoxToggled(livename,qChkBx_shot.text()))
grpLayout.addWidget(qChkBx_shot,QtCore.Qt.AlignCenter)
i +=1
groupBox.setLayout(grpLayout)
return groupBox
使用以下代码的 GroupBox:
def InitUi(self,livelist,shotlist):
scrolllayout = QtGui.QGridLayout()
scrollwidget = QtGui.QWidget()
scrollwidget.setLayout(scrolllayout)
scroll = QtGui.QScrollArea()
scroll.setWidgetResizable(True) # Set to make the inner widget resize with scroll area
scroll.setWidget(scrollwidget)
i=0
length = len(livelist)-2
x,y=0,0
while x <= math.ceil(length/4):
for y in range(0,4):
if (i < (length)):
groupbox=self.createGroupBox(livelist[i],shotlist)
self.groupboxes.append(groupbox)
scrolllayout.addWidget(groupbox, x, y)
y +=1
i +=1
x+=1
self.Okbutton = QtGui.QPushButton('OK',self)
self.Okbutton.clicked.connect(lambda: self.buttonPressed())
self.Okbutton.setMaximumWidth(100)
layout = QtGui.QVBoxLayout()
layout.addWidget(scroll)
layout.addWidget(self.Okbutton,QtCore.Qt.AlignRight)
self.setLayout(layout)
self.setWindowTitle("Customized LiveShotLiveSwitching")
self.resize(1200, 500)
self.show()
我的查询是,我可以检索哪个组框被激活的值,但我无法获取该组框下选中的复选框列表。
谁能帮我解决这个问题...
最佳答案
使组框成为每个复选框的父级:
qChkBx_shot = QtGui.QCheckBox("Shot-"+shotlist[i], groupBox)
现在您可以使用以下方法迭代组框的复选框:
for checkbox in groupbox.findChildren(QtGui.QCheckBox):
print('%s: %s' % (checkbox.text(), checkbox.isChecked()))
并获取复选框所属的组框:
groupbox = checkbox.parent()
关于python - 如何获取 PyQt 中 QGroupbox 中存在的 Qcheckbox 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22455606/