Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt GridLayout combining widgets

Tags:

pyqt

so for example, I have a GUI that has a label1+line_edit1, label2+line_edit2, button1, button2, button3. In a normal sense the code would look somewhat like this:

class gridlayout_example(QtGui.QWidget):
    def __init__(self):
        self.grid_layout = QtGui.QGridLayout()

        self.label1 = QtGui.QLabel("label1")
        self.grid_layout.addWidget(self.label1,0,0,1,3)

        self.line_edit1 = QtGui.QLineEdit()
        self.grid_layout.addWidget(self.line_edit1,1,0,1,3)

        self.label2 = QtGui.QLabel("label2")
        self.grid_layout.addWidget(self.label1,2,0,1,3)

        self.line_edit2 = QtGui.QLineEdit()
        self.grid_layout.addWidget(self.line_edit2,3,0,1,3)

        self.button1 = QtGui.QPushButton("button1")
        self.button2 = QtGui.QPushButton("button2")
        self.button3 = QtGui.QPushButton("button3")

        self.grid_layout.addWidget(self.button1, 4,0,1,1)
        self.grid_layout.addWidget(self.button2, 4,1,1,1)
        self.grid_layout.addWidget(self.button3, 4,2,1,1)

        self.setLayout(self.grid_layout)

But is there a way to combine label1+line_edit1 and label2 + line_edit2 so it becomes something like:

[label1                    
 line edit1               ] -> (0,0,1,3)
[label2                    
 line edit2               ] -> (1,0,1,3)
[button1][button2][button3] -> (2,x,1,1)

so basically label1+line edit1 would occupy row 0 of the grid layout, label2 + line edit2 occupy row1 and so on...

like image 852
Krin123 Avatar asked Oct 30 '14 22:10

Krin123


People also ask

How are widgets added to a Gridlayout?

The normal way to add a layout is by calling addLayout() on the parent layout. Once you have added your layout you can start putting widgets and other layouts into the cells of your grid layout using addWidget(), addItem(), and addLayout().

What is the correct method to add child layout is?

the correct method to add child layout is: a setlayout() b addlayout() c addwidget() The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.

How do I use QHBoxLayout?

First, we create the widgets we want in the layout. Then, we create the QHBoxLayout object and add the widgets into the layout. Finally, we call setLayout() to install the QHBoxLayout object onto the widget. At that point, the widgets in the layout are reparented to have window as their parent.


1 Answers

Create a second layout to use as a sublayout, add your widgets to it, and use addLayout() in place of addWidget()

class gridlayout_example(QtGui.QWidget):
    def __init__(self, parent=None):
        super(gridlayout_example, self).__init__(parent)

        label1 = QtGui.QLabel('label 1')
        line_edit1 = QtGui.QLineEdit()
        sublayout1 = QtGui.QVBoxLayout()
        sublayout1.addWidget(label1)
        sublayout1.addWidget(line_edit1)

        label2 = QtGui.QLabel('label 2')
        line_edit2 = QtGui.QLineEdit()
        sublayout2 = QtGui.QVBoxLayout()
        sublayout2.addWidget(label2)
        sublayout2.addWidget(line_edit2)

        button1 = QtGui.QPushButton("button1")
        button2 = QtGui.QPushButton("button2")
        button3 = QtGui.QPushButton("button3")

        grid_layout = QtGui.QGridLayout(self)
        grid_layout.addLayout(sublayout1, 0, 0, 1, 3)
        grid_layout.addLayout(sublayout2, 1, 0, 1, 3)
        grid_layout.addWidget(button1, 2, 0, 1, 1)
        grid_layout.addWidget(button2, 2, 1, 1, 1)
        grid_layout.addWidget(button3, 2, 2, 1, 1)
like image 77
user3419537 Avatar answered Nov 01 '22 21:11

user3419537