sizeHint of QCheckBox not correct.
-
What governs the sizes returned by the sizeHint function on a QCheckBox?
I am calling this for each checkbox in a layout to determine the size of the checkbox, however it doesn't seem to be correct and as a result the layout is to small to show all the text for all the checkboxes, one in particular is missing 4 letters from the end.
I'm using Qt 5.9.2
-
Hi
QSize QCheckBox::sizeHint() const { Q_D(const QCheckBox); if (d->sizeHint.isValid()) return d->sizeHint; ensurePolished(); QFontMetrics fm = fontMetrics(); QStyleOptionButton opt; initStyleOption(&opt); QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false, text()).size(); if (!opt.icon.isNull()) sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height())); d->sizeHint = (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this) .expandedTo(QApplication::globalStrut())); return d->sizeHint; }Icon and text and sizeFromContents
-
Hi
QSize QCheckBox::sizeHint() const { Q_D(const QCheckBox); if (d->sizeHint.isValid()) return d->sizeHint; ensurePolished(); QFontMetrics fm = fontMetrics(); QStyleOptionButton opt; initStyleOption(&opt); QSize sz = style()->itemTextRect(fm, QRect(), Qt::TextShowMnemonic, false, text()).size(); if (!opt.icon.isNull()) sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height())); d->sizeHint = (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this) .expandedTo(QApplication::globalStrut())); return d->sizeHint; }Icon and text and sizeFromContents
@mrjj , the text to go with the checkboxes:
Sensor Calibration Invalid Quadpack Temperature Out of Bounds Sensor Internal Fault Sensor Control Box DisconnectedThe text I see in the group box which contains the above checkboxes, the text showing:
Sensor Calibration Invalid Quadpack Temperature Out of Bo Sensor Internal Fault Sensor Control Box Disconnected -
@mrjj , the text to go with the checkboxes:
Sensor Calibration Invalid Quadpack Temperature Out of Bounds Sensor Internal Fault Sensor Control Box DisconnectedThe text I see in the group box which contains the above checkboxes, the text showing:
Sensor Calibration Invalid Quadpack Temperature Out of Bo Sensor Internal Fault Sensor Control Box DisconnectedWell sizeHint is only a hint.
Also if you try to resize the Group box based on sizeHints from the QComboxes
then make sure they are FULLY shown as reading it in the ctor will not always give correct results.Write a small test app where they are FULLY shown and see what values it returns before you think its wrong/incorrect. :)
-
Well sizeHint is only a hint.
Also if you try to resize the Group box based on sizeHints from the QComboxes
then make sure they are FULLY shown as reading it in the ctor will not always give correct results.Write a small test app where they are FULLY shown and see what values it returns before you think its wrong/incorrect. :)
-
@mrjj , I'm not sure I understand what you are saying, as I said the QGroupBox contains only the QCheckBoxes shown and isn't wide enough.
-
Hi
Ok then I dont know what is wrong :)You dont agree with the size it returns or ?
Sizehint is only a hint. the layout will control it.
@mrjj , the code:
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); } -
@mrjj , the code:
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); }Hi
It seems you init things here so they are not visible and hence
pchkbxAlarm->sizeHint() might not report right with layouts involved and its also inside a while loop so
most likely its the reason why they get too small. -
@mrjj , the code:
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); } -
-
@JonB But since the table is obtained inside the constructor, the maximum length of the texts can be extracted from font size and a minimum width can be set to the check boxes.