Question

I have a QTableWidget and 6 QPushBotton i want to put them in a layout in this way

enter image description here

just a small spacing between Bottons

But Using QGridLayout make it looks like this

enter image description here

Code:

    tab_Memory_layout = QtGui.QGridLayout()
    tab_Memory_layout.setVerticalSpacing(0)
    tab_Memory_layout.setAlignment(QtCore.Qt.AlignTop)
    tab_Memory_layout.addWidget(self.MemoryTable, 0, 0)
    tab_Memory_buttons = QtGui.QGridLayout()
    tab_Memory_buttons.setSpacing(5)
    tab_Memory_buttons.addWidget(self.pushButton_add_memory, 0, 0, QtCore.Qt.AlignTop)
    tab_Memory_buttons.addWidget(self.pushButton_up_memory, 1, 0, QtCore.Qt.AlignTop)
    tab_Memory_buttons.addWidget(self.pushButton_down_memory, 2, 0, QtCore.Qt.AlignTop)
    tab_Memory_buttons.addWidget(self.pushButton_top_memory, 3, 0, QtCore.Qt.AlignTop)
    tab_Memory_buttons.addWidget(self.pushButton_bottom_memory, 4, 0, QtCore.Qt.AlignTop)
    tab_Memory_buttons.addWidget(self.pushButton_delete_memory, 5, 0, QtCore.Qt.AlignTop)
    tab_Memory_layout.addLayout(tab_Memory_buttons, 0, 1)
    self.tab_Memory.setLayout(tab_Memory_layout)

I tried the setRowStretch(i, 0), setVerticalSpacing(0) but it didn't help!

I also used QGridLayout and QVBoxLayout for the Bottons Group, it works but all Buttons are aligned at the Bottom, although i used AlignTop for each element and for the Layout

Was it helpful?

Solution

You just need to add a Spacer in your layout:

enter image description here

The QSpacerItem class provides blank space in a layout.

verticalSpacer = QSpacerItem(40, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
tab_Memory_buttons.addItem(verticalSpacer, 6, 0, QtCore.Qt.AlignTop)

Any extra space in the layout will be "absorbed" by the spacer, which will keep other widgets aligned.

OTHER TIPS

Add a Qlabel("") below delete button. This makes them up like the way you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top