我正在尝试使用PyQT设计状态机(GroupBox中的GroupBox)
对于5个州,我有5个QGroupBox
,其中5个QGroupBox
首先需要具有相同的布局,其中4个QCheckBox
和一个QPushButton
排列在3 QHBoxLayout
内QVBoxLayout
,但第5 QGroupBox
只有一个按钮
我正在尝试执行代码,但是我将QCheckBox
和QPushButtons
放在一个组框中,我尝试了很多组合,但没有一个在{{1}中给我正确的排列}
QGroupBox
我希望前4 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
与第1个编辑的Groupbox相同
我可以创建单独的QGroupBox
没有循环,但为了减少长度我需要使用循环
答案 0 :(得分:1)
您可以像这样更改代码,尝试将按钮放在布局的中心
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
您可以访问每个Groupbox的按钮
第一组Box self.Applybutton[0]
对于第二个Groupbox来说self.Applybutton[1]
....同样