繁体   English   中英

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

[英]How to get the state of Qcheckbox that is present inside the QGroupbox in PyQt

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

这里我的查询是,我可以检索激活哪个 groupbox 的值,但我无法在该 groupbox 下检查复选框列表。

谁能帮我解决这个...

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

    qChkBx_shot = QtGui.QCheckBox("Shot-"+shotlist[i], groupBox)

现在,您可以使用以下命令遍历 groupbox 的复选框:

    for checkbox in groupbox.findChildren(QtGui.QCheckBox):
        print('%s: %s' % (checkbox.text(), checkbox.isChecked()))

并获取复选框所属的组框:

    groupbox = checkbox.parent()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM