Why is space before first button?
-
I have
my code:
groupBox1 = new QGroupBox(); groupBox1->setAlignment(Qt::AlignLeft); QHBoxLayout *horizLayout1 = new QHBoxLayout; QPushButton *btn1 = new QPushButton; btn1->setContentsMargins(0,0,0,0); btn1->setMaximumWidth(30); QPushButton *btn2 = new QPushButton; btn2->setMaximumWidth(30); QPushButton *btn3 = new QPushButton; btn3->setMaximumWidth(30); horizLayout1->addWidget(btn1); horizLayout1->addWidget(btn2); horizLayout1->addWidget(btn3); horizLayout1->addWidget(new QLineEdit); horizLayout1->addWidget(new QLineEdit); horizLayout1->setSpacing(0); horizLayout1->setMargin(0); groupBox1->setLayout(horizLayout1); QSplitter *splitter = new QSplitter(Qt::Orientation::Horizontal); splitter->addWidget(new QTreeView); splitter->addWidget(new QListView); .. QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(groupBox1); mainLayout->addWidget(splitter); mainLayout->setSpacing(0); setLayout(mainLayout);
Margins=0, spacing=0 but in left, upper corner is space
-
@AndrzejB thats very strange, I can confirm it on 5.15.8 and macOS, when you change the first item to a QLineEdit, everything lays out correctly.
I would consider this a bug (if no-one else has an idea/workaround) and would open a ticket over at https://bugreports.qt.io , if there isn't already one open for it.
-
I do not see this on Qt 5.15.2 Linux (The code does not compile under 6.2.3).
What I note though is:
- The QGroupBox border is absent in your screen shot. Something you have styled out or a Mac thing maybe?
- The example code does not try to set the QGroupBox contents margins. They will be non-zero (3 pixels left/right for me) but not, I expect, as large as the gap on your left. There is no content margin or space for a border on the right in your screen shot; I would have expected a gap.
If you are not going to use the group box title but need the visible border you could substitute QFrame. If you need neither the group box title nor the border then you can add the horizontal layout directly to the vertical layout and do without the container widget.