简体   繁体   English

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

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

My project contains Qdialog having multiple QGroupbox.Each GroupBox contains some number of checkboxes.The list of checkboxes is same for all the groupbox.我的项目包含具有多个 QGroupbox 的 Qdialog。每个 GroupBox 包含一定数量的复选框。所有 groupbox 的复选框列表都相同。 I dont have much reputation to load an image :(我没有太多的声誉来加载图像:(

Here the user is able to select the checkboxes based on his need and presses the ok button.Once the Ok Button is pressed, I should be able to get the list of checkboxes checked by the user.在这里,用户可以根据需要选择复选框并按下确定按钮。按下确定按钮后,我应该能够获得用户选中的复选框列表。

I am creating the checkboxes in a loop, here is the code:我在循环中创建复选框,这是代码:

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 with the below code:具有以下代码的 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()

Here my query is, I could able to retrive the value as which groupbox is been activated and I couldn't able to get the list of checkboxes checked under that groupbox.这里我的查询是,我可以检索激活哪个 groupbox 的值,但我无法在该 groupbox 下检查复选框列表。

Can anybody help me to solve this...谁能帮我解决这个...

Make the groupbox the parent of each checkbox:使 groupbox 成为每个复选框的父级:

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

Now you can iterate over the checkboxes of a groupbox with:现在,您可以使用以下命令遍历 groupbox 的复选框:

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

And to get the groupbox the checkbox belongs to:并获取复选框所属的组框:

    groupbox = checkbox.parent()

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

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