Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQT5: Grid layout inside horizontal layout

Can someone help me figure out how to combine layouts? Taking offset from the guides from: [https://pythonspot.com/en/pyqt5/]

I would rather not use Designer as the layout is going to be a part of several tabs that is determined based on the amount of tests and data sets from a specified data folder.

For example, I would like to switch out the Blue button in:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot

    class App(QDialog):

        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 layout - pythonspot.com'
            self.left = 10
            self.top = 10
            self.width = 320
            self.height = 100
            self.initUI()

        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)

            self.createHorizontalLayout()

            windowLayout = QVBoxLayout()
            windowLayout.addWidget(self.horizontalGroupBox)
            self.setLayout(windowLayout)

            self.show()

        def createHorizontalLayout(self):
            self.horizontalGroupBox = QGroupBox("What is your favorite color?")
            layout = QHBoxLayout()

            buttonBlue = QPushButton('Blue', self)
            buttonBlue.clicked.connect(self.on_click)
            layout.addWidget(buttonBlue) 

            buttonRed = QPushButton('Red', self)
            buttonRed.clicked.connect(self.on_click)
            layout.addWidget(buttonRed) 

            buttonGreen = QPushButton('Green', self)
            buttonGreen.clicked.connect(self.on_click)
            layout.addWidget(buttonGreen) 

            self.horizontalGroupBox.setLayout(layout)


        @pyqtSlot()
        def on_click(self):
            print('PyQt5 button click')

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())

with the grid layout from:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, QGridLayout
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot

    class App(QDialog):

        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 layout - pythonspot.com'
            self.left = 10
            self.top = 10
            self.width = 320
            self.height = 100
            self.initUI()

        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)

            self.createGridLayout()

            windowLayout = QVBoxLayout()
            windowLayout.addWidget(self.horizontalGroupBox)
            self.setLayout(windowLayout)

            self.show()

        def createGridLayout(self):
    self.horizontalGroupBox = QGroupBox("Grid")
    layout = QGridLayout()
    layout.setColumnStretch(1, 4)
    layout.setColumnStretch(2, 4)

    layout.addWidget(QPushButton('1'),0,0) 
    layout.addWidget(QPushButton('2'),0,1) 
    layout.addWidget(QPushButton('3'),0,2) 
    layout.addWidget(QPushButton('4'),1,0) 
    layout.addWidget(QPushButton('5'),1,1) 
    layout.addWidget(QPushButton('6'),1,2) 
    layout.addWidget(QPushButton('7'),2,0) 
    layout.addWidget(QPushButton('8'),2,1) 
    layout.addWidget(QPushButton('9'),2,2) 

    self.horizontalGroupBox.setLayout(layout)

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())        
like image 679
Max Møller Madsen Avatar asked Sep 21 '17 06:09

Max Møller Madsen


Video Answer


1 Answers

You can add a layout as an element of another layout in a similar fashion to adding a widget by using addLayout()

layout = QtWidgets.QHBoxLayout()
sublayout = QtWidgets.QGridLayout()
layout.addLayout(sublayout)
like image 89
user3419537 Avatar answered Sep 29 '22 08:09

user3419537