如何获取 PyQt 中 QGroupbox 内存在的 Qcheckbox 的状态

问题描述 投票:0回答:1

我的项目包含具有多个QGroupbox的Qdialog。每个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()

我的查询是,我可以检索激活哪个组框的值,但无法获取该组框下选中的复选框列表。

任何人都可以帮我解决这个问题吗...

python pyqt qcheckbox qgroupbox
1个回答
9
投票

使组框成为每个复选框的父级:

    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()
© www.soinside.com 2019 - 2024. All rights reserved.