Consider the case of a QMainWindow
with a QWidget
as central widget. This widget has a QHBoxLayout
. I add two other widgets to it, each with a QVBoxLayout
.
I now want to bring the Widgets inside QVBoxLayout
closer to each other. The attempt is to use .setMargin(0)
, .setSpacing(0)
and .setContentsMargins(0,0,0,0)
for this purpose.
However the result is that their separation is actually increased instead of decreased - as can be seen in the picture (where Gain is the widget where I set the margins and spacings to zero).
The code to reproduce this issue is appended below. (And the same actually happens when using a QGridLayout
.)
Here is the question on two different complexity levels:
(a) Since the only difference between the two widgets is that one has the margins and spacings set to zero, one of the used method calls must have done something else to the layout or widget-properties. Which other property is changed by setting any of .setMargin(0)
, .setSpacing(0)
and setContentsMargins(0,0,0,0)
?
(b) How do I make the spacing between the text label and the combobox in this example smaller?
from PyQt4 import QtGui
import sys
class LabeledComboBox(QtGui.QWidget):
def __init__(self, text="", items=[], parent=None):
super(LabeledComboBox, self).__init__(parent)
self.parent = parent
self.widgetlayout = QtGui.QVBoxLayout(self)
self.widgetlayout.addWidget(QtGui.QLabel(text))
self.Combo = QtGui.QComboBox()
self.Combo.addItems(items)
self.widgetlayout.addWidget(self.Combo)
self.parent.mylayout.addWidget(self)
def printParams(self):
# print some margin/spacing parameters for testing
m = self.widgetlayout.margin()
s = self.widgetlayout.spacing()
cm = self.widgetlayout.getContentsMargins()
print "margin: {m}, spacing: {s}, ContentsMargin: {cm}".format(m=m, s=s, cm=cm)
class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self.mainbox = QtGui.QWidget()
self.mylayout = QtGui.QHBoxLayout()
self.mainbox.setLayout(self.mylayout)
self.setCentralWidget(self.mainbox)
self.GainWidget = LabeledComboBox("Gain", ['low', 'medium', 'high'], self)
self.RevolutionsWidget = LabeledComboBox("Revolutions", ['100', '200', '400'], self)
self.GainWidget.printParams()
# this outputs: margin: 9, spacing: 6, ContentsMargin: (9, 9, 9, 9)
# now I set everything to zero
self.GainWidget.widgetlayout.setMargin(0)
self.GainWidget.widgetlayout.setSpacing(0)
self.GainWidget.widgetlayout.setContentsMargins(0,0,0,0)
# check
self.GainWidget.printParams()
# margin: 0, spacing: 0, ContentsMargin: (0, 0, 0, 0)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
thisapp = App()
thisapp.show()
sys.exit(app.exec_())