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. QGroupBox automatically sizing
Forum Updated to NodeBB v4.3 + New Features

QGroupBox automatically sizing

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 352 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

    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?

    Kind Regards,
    Sy

    raven-worxR 1 Reply Last reply
    0
    • SPlattenS SPlatten

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @SPlatten
      try

      pgrpbxAlarms->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      SPlattenS 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @SPlatten
        try

        pgrpbxAlarms->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #3

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

        Kind Regards,
        Sy

        raven-worxR 1 Reply Last reply
        0
        • SPlattenS SPlatten

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          SPlattenS 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @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);
            
            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @raven-worx , I think I'll stick with my fiddle factor:

            static const quint16 scuint16CheckBoxIndicatorWidth = 24;
            intAlarmsWidth += scuint16CheckBoxIndicatorWidth;
            

            Kind Regards,
            Sy

            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