我想将一个包含许多复选框和按钮小部件的组框放入 PyQt4 中的 GridLayout 中。另外,我希望在 groupBox 之外有一个按钮(但在相同的布局中),一旦选中了中的复选框,就可以通过单击它来运行某些功能。
另外,我想在 groupbox 上有一个滚动条(也许 QScrollArea 可以?),因为我将来可能会向它添加更多按钮。
需要注意的一件事是此布局/窗口中有多个选项卡(我不确定该术语)。所有的 groupbox/button/etc 都应该在 Tab 1 中。
我已经阅读了一些具有相同情况的文章,但没有一篇提供正确的答案......如果有人知道如何解决这个问题,请告诉我。赞赏!
编辑:我的代码如下。如您所见,我在底部有一个组框。我想要做的是将组框放在所有复选框/按钮的顶部,除了组框外底部的运行按钮。这些都应该只发生在 Tab 1 中。
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os, sys
class Page1 (QTabWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1, "Tab1")
self.addTab(self.tab2, "Tab2")
self.addTab(self.tab3, "Tab3")
self.tab1_initUI()
self.tab2_initUI()
self.tab3_initUI()
def tab1_initUI(self):
btn1a = QPushButton('A1')
btn1a.resize(btn1a.sizeHint())
btn1b = QPushButton('A2')
btn1b.resize(btn1b.sizeHint())
btn2a = QPushButton('B1')
btn2a.resize(btn2a.sizeHint())
btn2b = QPushButton('B2')
btn2b.resize(btn2b.sizeHint())
btn3a = QPushButton('C1')
btn3a.resize(btn3a.sizeHint())
btn3b = QPushButton('C2')
btn3b.resize(btn3b.sizeHint())
btn4a = QPushButton('D1', self)
btn4a.resize(btn4a.sizeHint())
btn4b =QPushButton('D2', self)
btn4b.resize(btn4b.sizeHint())
btn5a = QPushButton('E1', self)
btn5a.resize(btn5a.sizeHint())
btn5b = QPushButton('E2', self)
btn5b.resize(btn5b.sizeHint())
self.checkBox1 = QtGui.QCheckBox('1', self)
self.checkBox2 = QtGui.QCheckBox('2', self)
self.checkBox3 = QtGui.QCheckBox('3', self)
self.checkBox4 = QtGui.QCheckBox('4', self)
self.checkBox5 = QtGui.QCheckBox('5', self)
btnRun = QPushButton('Run', self)
btnRun.resize(btnRun.sizeHint())
grid = QtGui.QGridLayout()
grid.addWidget(self.checkBox1, 1, 0)
grid.addWidget(btn1a, 1, 1)
grid.addWidget(btn1b, 1, 2)
grid.addWidget(self.checkBox2, 2, 0)
grid.addWidget(btn2a, 2, 1)
grid.addWidget(btn2b, 2, 2)
grid.addWidget(self.checkBox3, 3, 0)
grid.addWidget(btn3a, 3, 1)
grid.addWidget(btn3b, 3, 2)
grid.addWidget(self.checkBox4, 4, 0)
grid.addWidget(btn4a, 4, 1)
grid.addWidget(btn4b, 4, 2)
grid.addWidget(self.checkBox5, 5, 0)
grid.addWidget(btn5a, 5, 1)
grid.addWidget(btn5b, 5, 2)
grid.addWidget(btnRun, 6, 0, 1, 3)
groupbox = QtGui.QGroupBox(self)
hbox = QtGui.QHBoxLayout()
grid2= QtGui.QGridLayout()
hbox.addLayout(grid2)
groupbox.setLayout(hbox)
scroll = QtGui.QScrollArea()
scroll.setWidget(groupbox)
scroll.setWidgetResizable(True)
grid.addWidget(groupbox,7,0,1,3)
self.tab1.setLayout(grid)
def tab2_initUI(self):
grid = QtGui.QGridLayout()
self.tab2.setLayout(grid)
def tab3_initUI(self):
grid = QtGui.QGridLayout()
self.tab3.setLayout(grid)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(300, 200, 600, 370)
self.startPage1()
def startPage1(self):
x = Page1(self)
self.setWindowTitle("Auto Benchmark")
self.setCentralWidget(x)
self.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()