c++qtlayoutqlayoutqgroupbox

Qt GUI : misaligned things in groupBox with horizontal Layout


Very simple :

Ui file : enter image description here

Result : enter image description here

"Scans to display" is a groupBox with a horizontal layout which contains two elements :

A red line (added manually on the screenshot) marks the misalignment between those two elements within the "Scans to display" group box.

Of note : the empty groupBox on the right gets automatically filled with numbered checkboxes on startup, the number of which is only known then.

What is going on ? What should I do ?

PS : code to create some number of checkboxes inside the empty groupbox on the right :

m_historyButtons[i] = new QCheckBox();
m_historyButtons[i]->setText(QString::number(i));
m_historyButtons[i]->setObjectName(QString("m_pbDisplayEntry%1").arg(i));

QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(true);
m_historyButtons[i]->setSizePolicy(sizePolicy);

((QGridLayout*)(ui.m_groupBoxHistoryEntries->layout()))->addWidget(m_historyButtons[i], i/m_grid_width, i%m_grid_width);

Solution

  • Qt groupboxes leave some room on top, outside the frame, for the groupbox caption. To avoid that, if you don't need a caption, use a QFrame, instead. Newly created checkboxes inside the frame will keep their mutually exclusive beahviour.