如何将GroupBox放在GridLayout中? PyQt4的

时间:2017-09-28 05:31:24

标签: python pyqt pyqt4 qscrollarea qgroupbox

我想将一个包含很多复选框和按钮小部件的组合框放入PyQt4中的GridLayout。此外,我想在groupBox外部有一个按钮(但是在相同的布局中),一旦选中了复选框,就可以通过单击它来运行某些功能。

另外,我想在组合框中有一个滚动条(可能是QScrollArea会这么做?),因为我将来可能会添加更多按钮。

有一点需要注意的是,这个布局/窗口中有多个标签(我不确定这个术语)。并且所有groupbox / button / etc都应该在Tab 1中。

我已经阅读了一些具有相同情况的文章,但没有一篇提供正确的答案......如果有人知道如何解决这个问题,请告诉我。理解!

编辑:我的代码如下。如您所见,我在底部有一个组合框。我想要做的是将组合框放在顶部,包含所有复选框/按钮,除了组框外部底部的“运行”按钮。这些都应该只发生在表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()

enter image description here

1 个答案:

答案 0 :(得分:1)

总是建议划分任务,在这种情况下,我们将创建班级Widget,我们将放置按钮和复选框。

class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QGridLayout())
        for i in range(20):
            letter = chr(ord('a') + i)
            checkBox = QtGui.QCheckBox('{}'.format(i+1), self)
            self.layout().addWidget(checkBox, i, 0)

            btna = QPushButton("{}1".format(letter), self)
            btnb = QPushButton("{}2".format(letter), self) 

            self.layout().addWidget(btna, i, 1)
            self.layout().addWidget(btnb, i, 2)

其次是实现包含Tab1 Widget和运行按钮的小部件的类QScrollArea

class Tab1(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QGridLayout())

        self.group = Widget(self)
        scroll = QtGui.QScrollArea(self)
        scroll.setWidget(self.group)
        scroll.setWidgetResizable(True)

        self.layout().addWidget(scroll)
        self.runBtn = QPushButton("Run", self)
        self.layout().addWidget(self.runBtn)

然后将按钮添加到QTabWidget(您不再需要功能tab1_initUI()

class Page1(QTabWidget):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.tab1 = Tab1()
        self.tab2 = QWidget()
        self.tab3 = QWidget()
        self.addTab(self.tab1, "Tab1")
        self.addTab(self.tab2, "Tab2")
        # ...

截图:

enter image description here

完整代码位于以下link