QGroupBox automatically sizing
Unsolved
General and Desktop
-
I want a instance of QGroupBox to be large enough to display a number of QCheckBoxes, here is my code:
//Create alarms group box QString strAlarmsGrp(DataSets::scstrTranslateText(DataSets::mscszAlarms)); QGroupBox* pgrpbxAlarms(new QGroupBox(strAlarmsGrp, this)); QVBoxLayout* pvbxAlarms(new QVBoxLayout); //Query database for alarm configuration QSqlQuery alarms("SELECT vcAlarm AS tag, vcDescription AS setting FROM alarms"); mlstAlarms.clear(); int intAlarmsWidth = 0; while( alarms.next() ) { QSqlRecord record(alarms.record()); QJsonObject objAlarm; for( int f=0; f<record.count(); f++ ) { QSqlField field(record.field(f)); QString strName(field.name()), strValue(field.value().toString()); objAlarm.insert(strName, QJsonValue(strValue)); } if ( objAlarm.isEmpty() != true ) { QString strAlarmText(objAlarm[DataSets::mscszSetting].toString()), strTag(objAlarm[DataSets::mscszTag].toString()), strText(DataSets::scstrTranslateText(strAlarmText)); QCheckBox* pchkbxAlarm(new QCheckBox(strText, this)); pchkbxAlarm->setEnabled(blnEnable); pchkbxAlarm->setProperty(DataSets::mscszTag, strTag); mlstAlarms.append(pchkbxAlarm); pvbxAlarms->addWidget(pchkbxAlarm); QSize szCheckbox(pchkbxAlarm->sizeHint()); if ( szCheckbox.isValid() == true && szCheckbox.width() > intAlarmsWidth ) { intAlarmsWidth = szCheckbox.width(); } } } if ( intAlarmsWidth > 0 ) { //Don't resize the alarms group box, default parameter is 0 pvbxAlarms->addStretch(); pgrpbxAlarms->setMaximumWidth(intAlarmsWidth); } pgrpbxAlarms->setLayout(pvbxAlarms);
The issue is that the group box isn't quite wide enough, the last couple of characters are missing. This could account for the checkbox itself, is there something additional I have to add for this?
-
@SPlatten
trypgrpbxAlarms->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
-
@raven-worx , thank you, just tried that adding:
//Create alarms group box QString strAlarmsGrp(DataSets::scstrTranslateText(DataSets::mscszAlarms)); QGroupBox* pgrpbxAlarms(new QGroupBox(strAlarmsGrp, this)); QVBoxLayout* pvbxAlarms(new QVBoxLayout); //Query database for alarm configuration QSqlQuery alarms("SELECT vcAlarm AS tag, vcDescription AS setting FROM alarms"); mlstAlarms.clear(); int intAlarmsWidth = 0; while( alarms.next() ) { QSqlRecord record(alarms.record()); QJsonObject objAlarm; for( int f=0; f<record.count(); f++ ) { QSqlField field(record.field(f)); QString strName(field.name()), strValue(field.value().toString()); objAlarm.insert(strName, QJsonValue(strValue)); } if ( objAlarm.isEmpty() != true ) { QString strAlarmText(objAlarm[DataSets::mscszSetting].toString()), strTag(objAlarm[DataSets::mscszTag].toString()), strText(DataSets::scstrTranslateText(strAlarmText)); QCheckBox* pchkbxAlarm(new QCheckBox(strText, this)); pchkbxAlarm->setEnabled(blnEnable); pchkbxAlarm->setProperty(DataSets::mscszTag, strTag); mlstAlarms.append(pchkbxAlarm); pvbxAlarms->addWidget(pchkbxAlarm); QSize szCheckbox(pchkbxAlarm->sizeHint()); if ( szCheckbox.isValid() == true && szCheckbox.width() > intAlarmsWidth ) { intAlarmsWidth = szCheckbox.width(); } } } pgrpbxAlarms->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); if ( intAlarmsWidth > 0 ) { //Don't resize the alarms group box, default parameter is 0 pvbxAlarms->addStretch(); pgrpbxAlarms->setMaximumWidth(intAlarmsWidth); } pgrpbxAlarms->setLayout(pvbxAlarms);
Unfortunately there is no difference. Is there a way to get the checkbox indicator size?
-
@SPlatten said in QGroupBox automatically sizing:
Is there a way to get the checkbox indicator size?
only when you subclass QGroupBox, since initStyleOption() method is protected.
QStyleOptionGroupBox opt; this->initStyleOption(&opt); QRect checkBoxRect = this->style()->subControlRect(QStyle::CC_GroupBox, &opt, QStyle::SC_GroupBoxCheckBox, this);
-
@raven-worx , I think I'll stick with my fiddle factor:
static const quint16 scuint16CheckBoxIndicatorWidth = 24; intAlarmsWidth += scuint16CheckBoxIndicatorWidth;