如何使用 python 中的循环将多个 Qwidget 放置在多个 Qgroupbox 中?

How can i place multiple Qwidgets in multiple Qgroupbox using a loop in python?

我正在尝试使用 PyQT 设计状态机(GroupBox 中的 GroupBox) 我有5个州的5个QGroupBox,其中5个QGroupBox第一个需要与4个QCheckBox和一个QPushButton排列在3个QHBoxLayout内相同的布局一个 QVBoxLayout 但第 5 个 QGroupBox 只有一个按钮

我试图执行代码,但我在一个组框中得到了所有 QCheckBoxQPushButtons,我尝试了很多组合,但其中 none 给了我正确的安排在 QGroupBox

def State_Machine(self):
    groupBox = QtGui.QGroupBox("State Machine")
    main_layout = QtGui.QGridLayout()
    States_list =["INIT","NORMAL","WAKE","SLEEP","STANDBY"]
    self.State =[]
    Regulators_list =["QCO","QVR","QT1","QT2"]
    self.Regulator_checkbox =[]
    self.Applybutton = []
    box1 = []
    box2_2 =[]
    box3_3 =[]
    box4_4 =[]
    box2 =QtGui.QHBoxLayout()
    box3 =QtGui.QHBoxLayout()
    box4 =QtGui.QVBoxLayout()

    for i in xrange(5):
        self.State.append(QtGui.QGroupBox(States_list[i]))
        self.Applybutton.append(QtGui.QPushButton("Apply"))
        box1.append(QtGui.QHBoxLayout())
        if i < 4 : # for the First 4 groupbox
            box4_4.append(QtGui.QVBoxLayout())
            print i
            for j in xrange(4):
                self.Checkbox = QtGui.QCheckBox(Regulators_list[j])
                box2_2.append(QtGui.QHBoxLayout())
                box3_3.append(QtGui.QHBoxLayout())
                if j < 2: # for placing 2 checkboxes in each QHBox
                    box2_2[j].addWidget(self.Checkbox)
                if j >= 2: # for placing 2 checkboxes in each QHBox
                    box3_3[j].addWidget(self.Checkbox)
                box4_4[i].addLayout(box2_2[i])
                box4_4[i].addLayout(box3_3[i])
            self.State[i].setLayout(box4_4[i])
        # self.State[i].setFixedSize(200,100)
        box1[i].addWidget(self.Applybutton[i])
        # self.State[i].setLayout(box1[i])

    main_layout.addWidget(self.State[0],4,2)
    main_layout.addWidget(self.State[1],2,2)
    main_layout.addWidget(self.State[2],0,2)
    main_layout.addWidget(self.State[3],2,4)
    main_layout.addWidget(self.State[4],2,0)
    groupBox.setLayout(main_layout)
    return groupBox

我希望前 4 个 QGroupBox 与第一个编辑的 Groupbox 相同 我可以创建没有循环的单独 QGroupBox,但为了减少我需要使用循环的长度

您可以像这样更改代码,尝试将按钮放在布局的中心

def State_Machine(self):
    groupBox = QtGui.QGroupBox("State Machine")
    main_layout = QtGui.QGridLayout()
    States_list =["INIT","NORMAL","WAKE","SLEEP","STANDBY"]
    self.State =[]
    self.Checkbox =[]
    Regulators_list =["QCO","QVR","QT1","QT2"]
    self.Regulator_checkbox =[]
    self.Applybutton = []

    box2 =QtGui.QHBoxLayout()
    box3 =QtGui.QHBoxLayout()

    label1 = QtGui.QLabel("EMPTY")
    box2.addWidget(label1)

    for i in xrange(5):
        self.State.append(QtGui.QGroupBox(States_list[i]))
        self.Applybutton.append(QtGui.QPushButton("Apply"))

        self.State[i].setFixedSize(200,100)

    for j in xrange(5):
        verticalbox = QtGui.QVBoxLayout()
        firsthbox = QtGui.QHBoxLayout()
        secondhbox = QtGui.QHBoxLayout()
        thirdhbox = QtGui.QHBoxLayout()
        firsthbox.addWidget(self.Applybutton[j])
        for  k in xrange(4):
            self.Checkbox = QtGui.QCheckBox(Regulators_list[k])
            if k < 2:
                secondhbox.addWidget(self.Checkbox)
            if k >= 2:
                thirdhbox.addWidget(self.Checkbox)

        verticalbox.addLayout(firsthbox)
        if j < 4:
            verticalbox.addLayout(secondhbox)
            verticalbox.addLayout(thirdhbox)
        if j == 4:
            verticalbox.addLayout(box2)


        self.State[j].setLayout(verticalbox)
    main_layout.addWidget(self.State[0],4,2)
    main_layout.addWidget(self.State[1],2,2)
    main_layout.addWidget(self.State[2],0,2)
    main_layout.addWidget(self.State[3],2,4)
    main_layout.addWidget(self.State[4],2,0)
    groupBox.setLayout(main_layout)
    return groupBox

您可以通过以下方式访问每个组框的按钮 self.Applybutton[0] 第一个组框
self.Applybutton[1] 对于第二个 Groupbox .... 同样