pythonpyqtpyqt4qscrollareaqgroupbox

How to put a GroupBox inside a GridLayout? PyQt4


I want to put a groupbox, which contains a lot of checkbox and button widgets in it, into a GridLayout in PyQt4. Also, I want to have a button outside the groupBox (but is in the same layout) that can run some functions by clicking it once the checkbox(s) in the is checked.

Also, I would like to have a scroll bar (maybe a QScrollArea would do?) to the groupbox due to I may add more buttons to it in the future.

One thing to be noticed is that there are multiple tabs in this layout/window (I'm not sure the term). And all the groupbox/button/etc should all in Tab 1.

I've read a few articles that have the same situations, but none of them provide a proper answer... If anyone knows how to solve the issue, pls let me know. Appreciated!

EDIT: My codes are as below. As you can see, I have a groupbox in the bottom. What I want to do is put the groupbox on the top with all the checkboxs/buttons, except a Run button at the bottom outside the groupbox. And these should all happen only in 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()

enter image description here


Solution

  • It is always advisable to divide the tasks, in this case we will create the class Widget where we will place the buttons and checkbox.

    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)
    

    Second is the class Tab1 that implements the widget that contains the Widget with the QScrollArea and the run button:

    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)
    

    Then add the button to the QTabWidget (you no longer need the function 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")
            # ...
    

    Screenshot:

    enter image description here

    The complete code is in the following link