Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. sizeHint of QCheckBox not correct.
Forum Updated to NodeBB v4.3 + New Features

sizeHint of QCheckBox not correct.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    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

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #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

      SPlattenS 1 Reply Last reply
      0
      • mrjjM mrjj

        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

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @mrjj , the text to go with the checkboxes:

        Sensor Calibration Invalid
        Quadpack Temperature Out of Bounds
        Sensor Internal Fault
        Sensor Control Box Disconnected
        

        The 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
        

        Kind Regards,
        Sy

        mrjjM 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @mrjj , the text to go with the checkboxes:

          Sensor Calibration Invalid
          Quadpack Temperature Out of Bounds
          Sensor Internal Fault
          Sensor Control Box Disconnected
          

          The 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
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @SPlatten

          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. :)

          SPlattenS 1 Reply Last reply
          0
          • mrjjM mrjj

            @SPlatten

            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. :)

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @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.

            Kind Regards,
            Sy

            mrjjM 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @SPlatten

              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.

              SPlattenS 1 Reply Last reply
              0
              • mrjjM mrjj

                @SPlatten

                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.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #7

                @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);
                    }
                

                Kind Regards,
                Sy

                mrjjM JonBJ 2 Replies Last reply
                0
                • SPlattenS SPlatten

                  @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);
                      }
                  
                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  1
                  • SPlattenS SPlatten

                    @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);
                        }
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @SPlatten
                    As @mrjj has pointed out, my rule-of-thumb for anything to do with Qt sizes is: only works correctly in/after events QWidget::show() or QWidget::resize(). Empirically discovered to be "rubbish" outside of those, e.g. in constructors.

                    JoeCFDJ 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @SPlatten
                      As @mrjj has pointed out, my rule-of-thumb for anything to do with Qt sizes is: only works correctly in/after events QWidget::show() or QWidget::resize(). Empirically discovered to be "rubbish" outside of those, e.g. in constructors.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #10

                      @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.

                      JonBJ 1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @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.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @JoeCFD
                        Yes, you can indeed do a calculation like that based on a font. But anything to do with, say, layouts is undefined.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved