簡體   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