How to prevent checkbox text from overlapping when multiple checkboxes in QHBoxLayout?
-
wrote on 5 Feb 2025, 09:07 last edited by RickyRister 2 May 2025, 09:08
In the minimal reproducible example below, the text of the checkbox is overlapping with the next checkbox over. Is there a way to prevent the text from overlapping?
The checkbox row is an
QHBoxLayout
that contains threeQCheckBox
plus a stretch . I want the checkboxes to be left-aligned in the layout, so I added a stretch to the right side. However, that seems to cause the checkbox text to overlap with the next checkbox over. Increasing the spacing or the content margins does not fix the issue.Here is the code
QVBoxLayout layout = QVBoxLayout(this); layout.setSpacing(0); layout.setContentsMargins(9, 0, 9, 5); setLayout(&layout); QHBoxLayout row1Layout = QHBoxLayout(this); row1Layout.setSpacing(3); row1Layout.setContentsMargins(9, 0, 9, 0); QLabel testLabel = QLabel("this a QLabel on row 1 that is longer than the next row =============="); row1Layout.addWidget(&testLabel); layout.addLayout(&row1Layout); QHBoxLayout row2Layout = QHBoxLayout(this); row2Layout.setContentsMargins(9, 0, 9, 0); QCheckBox checkbox1 = QCheckBox(this); QCheckBox checkbox2 = QCheckBox(this); QCheckBox checkbox3 = QCheckBox(this); checkbox1.setText(tr("checkbox 1")); checkbox2.setText(tr("checkbox 2 + text")); checkbox3.setText(tr("checkbox 3")); row2Layout.addWidget(&checkbox1); row2Layout.addWidget(&checkbox2); row2Layout.addWidget(&checkbox3); row2Layout.addStretch(); layout.addLayout(&row2Layout); QDialog *dlg = new QDialog(this); dlg->setLayout(&layout); dlg->exec();
-
In the minimal reproducible example below, the text of the checkbox is overlapping with the next checkbox over. Is there a way to prevent the text from overlapping?
The checkbox row is an
QHBoxLayout
that contains threeQCheckBox
plus a stretch . I want the checkboxes to be left-aligned in the layout, so I added a stretch to the right side. However, that seems to cause the checkbox text to overlap with the next checkbox over. Increasing the spacing or the content margins does not fix the issue.Here is the code
QVBoxLayout layout = QVBoxLayout(this); layout.setSpacing(0); layout.setContentsMargins(9, 0, 9, 5); setLayout(&layout); QHBoxLayout row1Layout = QHBoxLayout(this); row1Layout.setSpacing(3); row1Layout.setContentsMargins(9, 0, 9, 0); QLabel testLabel = QLabel("this a QLabel on row 1 that is longer than the next row =============="); row1Layout.addWidget(&testLabel); layout.addLayout(&row1Layout); QHBoxLayout row2Layout = QHBoxLayout(this); row2Layout.setContentsMargins(9, 0, 9, 0); QCheckBox checkbox1 = QCheckBox(this); QCheckBox checkbox2 = QCheckBox(this); QCheckBox checkbox3 = QCheckBox(this); checkbox1.setText(tr("checkbox 1")); checkbox2.setText(tr("checkbox 2 + text")); checkbox3.setText(tr("checkbox 3")); row2Layout.addWidget(&checkbox1); row2Layout.addWidget(&checkbox2); row2Layout.addWidget(&checkbox3); row2Layout.addStretch(); layout.addLayout(&row2Layout); QDialog *dlg = new QDialog(this); dlg->setLayout(&layout); dlg->exec();
wrote on 5 Feb 2025, 12:27 last edited by Pl45m4 2 May 2025, 12:28Sorry but cannot reproduce it...
(used nothing but your code on Windows 10, mscv, Qt 6.8)What setup are you using?
-
Sorry but cannot reproduce it...
(used nothing but your code on Windows 10, mscv, Qt 6.8)What setup are you using?
-
@Pl45m4
As you can see, OP seems to be using Mac and a rather larger font that yours. maybe the issue is there somewhere?wrote on 5 Feb 2025, 13:20 last edited by@JonB said in How to prevent checkbox text from overlapping when multiple checkboxes in QHBoxLayout?:
rather larger font that yours
Then there is no reproducible example above :)
And you cannot assume from the description that this is an issue only on Mac :) -
Sorry but cannot reproduce it...
(used nothing but your code on Windows 10, mscv, Qt 6.8)What setup are you using?
wrote on 6 Feb 2025, 03:43 last edited byWhat setup are you using?
MacOS 15.1
Apple clang
Qt 6.7QApplication::font()
returnsQFont(.AppleSystemUIFont,13,-1,5,400,0,0,0,0,0,0,0,0,0,0,1)
.
1/5