QComboBox can't set margins with setContentsMargins()
-
Hello, I have a window with a
QVBoxLayout
and inside it I have someQHBoxLayout
to set my widgets. All the widgets on the left have aQt.AlignRight
and all the widgets on the right have aQt.AlignLeft
so they are centered. Then I added some margins to separate a bit right widgets from left widgets. To do that I usesetContentMargins()
but for the QComboBox it doesn't work.#Here I create the first QLabel and the QLineEdit hlayout = QHBoxLayout() self.ensayoLabel = QLabel("Nombre del ensayo") self.nombreEnsayoInput = QLineEdit("") self.nombreEnsayoInput.setFixedSize(150, 25) #I set some left margin to the QLineEdit so it is a bit separated from the QLabel self.nombreEnsayoInput.setContentsMargins(10, 0, 0, 0) hlayout.addWidget(self.ensayoLabel) hlayout.addWidget(self.nombreEnsayoInput) hlayout.setAlignment(self.ensayoLabel, Qt.AlignRight) hlayout.setAlignment(self.nombreEnsayoInput, Qt.AlignLeft) layout.addLayout(hlayout) #This is the vertical layout of the window . . . . #Here I create a QCheckBox and a QComboBox h4layout = QHBoxLayout() self.rotacionArenaCheckBox = QCheckBox("Rotar arena") self.rotacionArenaCheckBox.stateChanged.connect(lambda: self.rotacionArenaCheckboxChanged()) self.direccionRotacionArenaCombobox = QComboBox() self.direccionRotacionArenaCombobox.addItem("Ateatorio") self.direccionRotacionArenaCombobox.addItem("Norte") self.direccionRotacionArenaCombobox.addItem("Sur") self.direccionRotacionArenaCombobox.addItem("Este") self.direccionRotacionArenaCombobox.addItem("Oeste") #I add some left margin to the QComboBox to separate it a bit from the QCheckBox, but it doesn't work self.direccionRotacionArenaCombobox.setContentsMargins(10, 0, 0, 0) self.direccionRotacionArenaCombobox.setEnabled(False) h4layout.addWidget(self.rotacionArenaCheckBox) h4layout.addWidget(self.direccionRotacionArenaCombobox) h4layout.setAlignment(self.rotacionArenaCheckBox, Qt.AlignRight) h4layout.setAlignment(self.direccionRotacionArenaCombobox, Qt.AlignLeft)
What is happening?
-
Hi,
From the looks of it, you could simplify your code using QFormLayout.
-
@SGaist said in QComboBox can't set margins with setContentsMargins():
Hi,
From the looks of it, you could simplify your code using QFormLayout.
@JoeCFD said in QComboBox can't set margins with setContentsMargins():
The first 4 rows can be laid out with grid layout. Then align widgets in the first column to the right and the widgets in the second column to the left.
Hello, thanks for yous answers, I will try both solution and let you know the results.
-
After some tries, using
QGridLayout
for the first 4 rows andQHBoxLayout
for the rest is the best solution as I need multiple columns in some rows. I get this result but now I want to add some vertical space between grids to separate them a bit.setContentMargins()
does not work neither in QGridLayout. What can I use?
Thanks