user2345
user2345

Reputation: 537

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

I am trying to design a state machine using PyQT ( GroupBox within GroupBox) i have 5 QGroupBox for 5 states, among the 5 QGroupBox the first needs to have same layout with 4 QCheckBox and one QPushButton arranged in 3 QHBoxLayout within a QVBoxLayout but the 5th QGroupBox has only one button

I was trying to execute the code but i get all my QCheckBox and QPushButtons in a single groupbox, I tried many combinations but none of them gave me the correct arrangement in the 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

enter image description here

I want the First 4 QGroupBox to be as same as the 1st editied Groupbox I can create separate QGroupBox without loops, but to reduce the length i need to use loops

Upvotes: 0

Views: 839

Answers (1)

user2345
user2345

Reputation: 126

You can change the code like this, try placing the buttons in the center of the layout

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

your state machine

you can access the buttons for each Groupbox by self.Applybutton[0] for the 1st Groupbox
self.Applybutton[1] for the 2st Groupbox .... similarly

Upvotes: 1

Related Questions