How Not to space the widgets within QHBoxLayout
pyqt, python
Solution
Your approach was already the right one. No contents margins on the layout as well as no spacing on the layout will put the buttons extremely close with a spacing of about 2 pixels. Negative margins set by a stylesheet can get the buttons further together but I don't recommend it because it doesn't look nice.
from PySide import QtGui
app = QtGui.QApplication([])
window = QtGui.QWidget()
window.setStyleSheet('QPushButton{margin-left:-1px;}') # remove this line if you want to have a tiny bit of spacing left
layout = QtGui.QHBoxLayout(window)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(QtGui.QPushButton('Button'))
layout.addWidget(QtGui.QPushButton('Button'))
layout.addWidget(QtGui.QPushButton('Button'))
layout.addWidget(QtGui.QPushButton('Button'))
layout.addWidget(QtGui.QPushButton('Button'))
layout.addWidget(QtGui.QPushButton('Button'))
window.show()
app.exec_()
Problem
While using: ``` layout = QtGui.QHBoxLayout() layout.addWidget(QtGui.QPushButton()) layout.addWidget(QtGui.QPushButton()) layout.addWidget(QtGui.QPushButton()) ``` the buttons get automatically spaced out within the width of the QHBoxLayout. Instead I would like the buttons to be placed edge by edge next to each other. I have tried to use : ``` layout.setContentsMargins(0, 0, 0, 0) layout.importLayout.setSpacing(0) ``` but it has no effect on buttons spacing. What attribute of the `QHBoxLayout` needs to be set to override the auto spacing?