2

How can I remove the spacing between buttons after fixing their size?

A space is added whenever I set the button's size.

I tried setSpacing but it does not work. Can this be done by sizePolicy or something else?

Here is my code:

from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setMinimumSize(QSize(800, 600))
        self.setWindowTitle('Horizontal Layout')

        self.container = QWidget()
        self.horizontalLayout = QHBoxLayout()

        self.button_1 = QPushButton('Button 1')
        self.button_1.setFixedSize(QSize(70, 60))
        self.button_2 = QPushButton('Button 2')
        self.button_2.setFixedSize(QSize(70, 60))
        self.button_3 = QPushButton('Button 3')
        self.button_3.setFixedSize(QSize(70, 60))

        self.horizontalLayout.addWidget(self.button_1)
        self.horizontalLayout.addWidget(self.button_2)
        self.horizontalLayout.addWidget(self.button_3)

        self.container.setLayout(self.horizontalLayout)
        self.setCentralWidget(self.container)


app = QApplication([])
window = MainWindow()
window.show()
app.exec()
The Amateur Coder
  • 789
  • 3
  • 11
  • 33
  • Well, you did `self.setMinimumSize()`, and there's no trace of `setSpacing()` nor alignment in your code, so the layout manager properly spaces items according to the possible size occupied by them, and based on the *available* size. What was the expected result? – musicamante May 06 '22 at 04:58
  • @musicamante Now I removed setMinimumSize() and added `self.horizontalLayout.setContentsMargins(0, 0, 0, 0)` and `self.horizontalLayout.setSpacing(0)` But there is still space when window is resized. I created the interface in Tkinter and now I am converting it to PySide6. Here is its image. [link](https://imgur.com/S5qgZFf) – Spark Drago May 06 '22 at 15:31
  • Use [`addStretch()`](https://doc.qt.io/qt-6/qboxlayout.html#addStretch) *before* and/or *after* the buttons that you want to be placed side by side. I suggest you to do some experiments using [layouts in Designer](https://doc.qt.io/qt-5/designer-layouts.html) so that you better understand how layout managers work in Qt. – musicamante May 06 '22 at 16:29
  • Thanks, you solved my problem. It worked. – Spark Drago May 06 '22 at 16:59

1 Answers1

1

Use addStretch() before and/or after the buttons. You can find more here.

Example:

from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QMainWindow,QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setMinimumSize(QSize(600, 400))
        self.move(600, 300)
        self.setWindowTitle('Horizontal Layout')

        self.container = QWidget()
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(0)

        self.button_1 = QPushButton('Button 1')
        self.button_1.setFixedSize(QSize(70, 60))
        self.button_2 = QPushButton('Button 2')
        self.button_2.setFixedSize(QSize(70, 60))
        self.button_3 = QPushButton('Button 3')
        self.button_3.setFixedSize(QSize(70, 60))
        self.button_4 = QPushButton('Button 4')
        self.button_4.setFixedSize(QSize(70, 60))
        self.button_5 = QPushButton('Button 5')
        self.button_5.setFixedSize(QSize(70, 60))
        self.button_6 = QPushButton('Button 6')
        self.button_6.setFixedSize(QSize(70, 60))
        self.button_7 = QPushButton('Button 7')
        self.button_7.setFixedSize(QSize(70, 60))

        self.horizontalLayout.addWidget(self.button_1)
        self.horizontalLayout.addWidget(self.button_2)
        self.horizontalLayout.addWidget(self.button_3)
        self.horizontalLayout.addStretch()
        self.horizontalLayout.addWidget(self.button_4)
        self.horizontalLayout.addWidget(self.button_5)
        self.horizontalLayout.addStretch()
        self.horizontalLayout.addWidget(self.button_6)
        self.horizontalLayout.addWidget(self.button_7)

        self.setLayout(self.horizontalLayout)


app = QApplication([])
window = MainWindow()
window.show()
app.exec()

This gives the following result: QHBoxLayout

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 11 '22 at 07:05