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. QButtonGroup reduce width around content?
Forum Updated to NodeBB v4.3 + New Features

QButtonGroup reduce width around content?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 706 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've found that QButtonGroup is very much wider than the content, is there any way to get the group to shrink wrap around the content?

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I've found that QButtonGroup is very much wider than the content, is there any way to get the group to shrink wrap around the content?

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

      @SPlatten
      I think you will be challenged on this.

      QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container

      My bold. QButtonGroup just doesn't have any visual significance. Look at its documentation.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        QButtonGroup has no visual representation. Are you talking about QGroupBox ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        SPlattenS 1 Reply Last reply
        1
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          It is a long day. He needs more coffee now.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            QButtonGroup has no visual representation. Are you talking about QGroupBox ?

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

            @JonB , @SGaist , I've applied a style to the instance of QButtonGroup using the setStyleSheet function, this gives it a visual appearance.

            Sorry it isn't the QButtonGroup that the style sheet is applied to its the Layout its in, the structure is:

            QGridLayout
            QButtonGroup
            QRadioButton
            QRadioButton

            So same question can the layout width be string wrapped around the buttons ?

            Kind Regards,
            Sy

            JonBJ 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Show your code. By the way, QButtonGroup does not inherit QWidget.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • SPlattenS SPlatten

                @JonB , @SGaist , I've applied a style to the instance of QButtonGroup using the setStyleSheet function, this gives it a visual appearance.

                Sorry it isn't the QButtonGroup that the style sheet is applied to its the Layout its in, the structure is:

                QGridLayout
                QButtonGroup
                QRadioButton
                QRadioButton

                So same question can the layout width be string wrapped around the buttons ?

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

                @SPlatten said in QButtonGroup reduce width around content?:

                Sorry it isn't the QButtonGroup that the style sheet is applied to its the Layout its in

                You can't apply stylesheet to a layout either, only to a widget. You really ought show the QSS you claim to be setting.

                SPlattenS 1 Reply Last reply
                0
                • JonBJ JonB

                  @SPlatten said in QButtonGroup reduce width around content?:

                  Sorry it isn't the QButtonGroup that the style sheet is applied to its the Layout its in

                  You can't apply stylesheet to a layout either, only to a widget. You really ought show the QSS you claim to be setting.

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

                  @JonB , ok, this is my class prototype:

                      typedef std::map<const QString, QButtonGroup*> mpGroups;
                      
                          class clsQtLayout : public QWidget, public clsXMLinterface {
                          private:
                              mpGroups* mmpGroups;
                              QLayout* mpobjLO;
                              QScrollArea* mpobjScroller;        
                      
                          public:
                              explicit clsQtLayout(clsXMLnode* pobjNode, QString* pstrCSS
                                                                       , QWidget* pobjParent);
                              ~clsQtLayout();
                              void addButton(QAbstractButton* pobjButton, const QString& crstrGroup);
                              void addWidget(QWidget *pobjWidget);
                              bool blnIsLayout() { return true; }
                              QButtonGroup* pobjAddGroup(const QString& crstrGroup);
                              QLayout* pobjGetLayout() { return mpobjLO; }
                              QScrollArea* pobjGetScroller() { return mpobjScroller; }        
                          }; 
                  

                  Implementation:

                  /**
                   * @brief clsQtLayout::clsQtLayout
                   * @param pobjNode : Pointer to XML node
                   * @param pstrCSS : Pointer to CSS
                   * @param pobjParent : Pointer to parent widget
                   */
                  clsQtLayout::clsQtLayout(clsXMLnode* pobjNode, QString* pstrCSS
                                                               , QWidget* pobjParent)
                                                      : QWidget(pobjParent)
                                                      , clsXMLinterface(pobjNode)
                                                      , mmpGroups(nullptr)
                                                      , mpobjLO(nullptr) {
                      Q_ASSERT_X(pobjNode!=nullptr, QString("%1::%1").arg(metaObject()->className()
                                                                         ,metaObject()->className())
                                                      .toLatin1().data()
                                                  , "No node supplied!");
                      QString strID(pobjNode->strGetAttribute(clsXMLnode::mscszAttrID))
                             ,strHeight(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrHeight))
                             ,strHorzSpacing(mpobjNode->strGetAttribute(
                                                              clsXMLnode::mscszAttrSpacingH))
                             ,strName
                             ,strType(pobjNode->strGetAttribute(clsXMLnode::mscszAttrType))
                             ,strVertSpacing(mpobjNode->strGetAttribute(
                                                              clsXMLnode::mscszAttrSpacingV))
                             ,strWidth(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrWidth));
                      strName = QString("%1: %2").arg(clsXMLnode::mscszNameLayout, strType);
                      if ( strID.isEmpty() != true ) {
                          strName += QString(", %1: %2").arg(clsXMLnode::mscszNameID, strID);
                      }
                      clsXMLnode::setObjName(*this, strName);
                      if ( strType.compare(clsXMLnode::mscszLayoutForm) == 0 ) {
                      //Set-up form
                          QFormLayout* pobjForm(new QFormLayout);
                          clsXMLnode::setObjName(*pobjForm, QString("%1, %2").arg(strName
                                                          , pobjForm->metaObject()->className()));
                          pobjForm->setContentsMargins(0,0,0,0);
                          pobjForm->setSpacing(0);
                          setLayout(pobjForm);
                      //Any spacing to apply?
                          if ( strHorzSpacing.isEmpty() != true ) {
                              int intHorzSpacing(strHorzSpacing.toInt());
                              if ( intHorzSpacing >= 0 ) {
                                  pobjForm->setHorizontalSpacing(intHorzSpacing);
                              }
                          }
                          if ( strVertSpacing.isEmpty() != true ) {
                              int intVertSpacing(strVertSpacing.toInt());
                              if ( intVertSpacing >= 0 ) {
                                  pobjForm->setVerticalSpacing(intVertSpacing);
                              }
                          }
                          mpobjLO = pobjForm;
                  
                      } else if ( strType.compare(clsXMLnode::mscszLayoutGrid) == 0 ) {
                          QGridLayout* pobjGrid(new QGridLayout);
                          clsXMLnode::setObjName(*pobjGrid, QString("%1, %2").arg(strName
                                                          , pobjGrid->metaObject()->className()));
                          pobjGrid->setContentsMargins(0,0,0,0);
                          pobjGrid->setSpacing(0);
                          setLayout(pobjGrid);
                          mpobjLO = pobjGrid;
                      } else if ( strType.compare(clsXMLnode::mscszLayoutHorizontal) == 0 ) {
                          QHBoxLayout* pobjHBox(new QHBoxLayout);
                          clsXMLnode::setObjName(*pobjHBox, QString("%1, %2").arg(strName
                                                          , pobjHBox->metaObject()->className()));
                          pobjHBox->setContentsMargins(0,0,0,0);
                          pobjHBox->setSpacing(0);
                          setLayout(pobjHBox);
                          mpobjLO = pobjHBox;
                      } else if ( strType.compare(clsXMLnode::mscszLayoutVertical) == 0 ) {
                          QVBoxLayout* pobjVBox(new QVBoxLayout);
                          clsXMLnode::setObjName(*pobjVBox, QString("%1, %2").arg(strName
                                                          , pobjVBox->metaObject()->className()));
                          pobjVBox->setContentsMargins(0,0,0,0);
                          pobjVBox->setSpacing(0);
                          setLayout(pobjVBox);
                          mpobjLO = pobjVBox;
                      }
                      mpobjScroller = new QScrollArea;
                      clsXMLnode::setObjName(*mpobjScroller, QString("%1, %2").arg(strName
                                              , mpobjScroller->metaObject()->className()));
                      mpobjScroller->setWidget(this);
                      if ( strHeight.isEmpty() != true ) {
                          int intHeight(strHeight.toInt());
                          if ( intHeight >= 0 ) {
                              mpobjScroller->setFixedHeight(intHeight);
                          }
                      }
                      if ( strWidth.isEmpty() != true ) {
                          int intWidth(strWidth.toInt());
                          if ( intWidth >= 0 ) {
                              mpobjScroller->setFixedWidth(intWidth);
                          }
                      }
                      if ( pstrCSS != nullptr && pstrCSS->isEmpty() != true ) {
                          mpobjScroller->setStyleSheet(*pstrCSS);
                      }
                      mpobjScroller->setWidgetResizable(true);
                  }
                  /**
                   * @brief clsQtLayout::~clsQtLayout
                   */
                  clsQtLayout::~clsQtLayout() {
                      if ( mmpGroups != nullptr ) {
                          for( mpGroups::iterator itGrp=mmpGroups->begin();
                               itGrp!=mmpGroups->end(); itGrp++ ) {
                              QButtonGroup* pobjBtnGrp(itGrp->second);
                              pobjBtnGrp->deleteLater();
                          }
                          delete mmpGroups;
                      }
                      if ( mpobjScroller != nullptr ) {
                          mpobjScroller->deleteLater();
                      }
                      if ( mpobjLO != nullptr ) {
                          mpobjLO->deleteLater();
                      }
                  }
                  /**
                   * @brief clsQtLayout::addButton
                   * @param pobjButton : Pointer to button
                   * @param crstrGroup : Constant reference to button group
                   */
                  void clsQtLayout::addButton(QAbstractButton* pobjButton, const QString& crstrGroup) {
                      if ( pobjButton == nullptr ) {
                          return;
                      }
                      if ( crstrGroup.isEmpty() != true ) {
                          QButtonGroup* pobjBtnGrp(pobjAddGroup(crstrGroup));
                          if ( pobjBtnGrp != nullptr ) {
                              pobjBtnGrp->addButton(pobjButton);
                          }
                      }        
                      if ( mpobjLO != nullptr && mpobjNode != nullptr ) {
                          QString strType(mpobjNode->strGetAttribute(clsXMLnode::mscszAttrType));
                          if ( strType.compare(clsXMLnode::mscszLayoutForm) == 0 ) {
                              QFormLayout* pobjForm(qobject_cast<QFormLayout*>(mpobjLO));
                              if ( pobjForm != nullptr ) {
                                  pobjForm->addWidget(pobjButton);
                              }
                          } else if ( strType.compare(clsXMLnode::mscszLayoutGrid) == 0 ) {
                              QGridLayout* pobjGrid(qobject_cast<QGridLayout*>(mpobjLO));
                              if ( pobjGrid == nullptr ) {
                                  return;
                              }
                      //Get a pointer to the widget based on its type
                              clsXMLinterface* pobjInterface(reinterpret_cast<clsXMLinterface*>
                                                                              (pobjButton));
                              clsXMLnode* pobjNode(nullptr);
                              if ( pobjInterface != nullptr ) {
                                  pobjNode = pobjInterface->pobjGetNode();
                              }
                              if ( pobjNode == nullptr ) {
                                  return;
                              }
                              QString strColumnNo(pobjNode->strGetAttribute(
                                                                  clsXMLnode::mscszAttrColumnNo))
                                     ,strRowNo(pobjNode->strGetAttribute(
                                                                  clsXMLnode::mscszAttrRowNo));
                              int intColumn(-1), intRow(-1);
                              if ( strColumnNo.isEmpty() != true ) {
                                  intColumn = strColumnNo.toInt();
                              }
                              if ( strRowNo.isEmpty() != true ) {
                                  intRow = strRowNo.toInt();
                              }
                              if ( intColumn != -1 && intRow != -1 ) {
                                  pobjGrid->addWidget(pobjButton, intRow, intColumn);
                              }
                          } else if ( strType.compare(clsXMLnode::mscszLayoutHorizontal) == 0 ) {
                              QHBoxLayout* pobjHBox(qobject_cast<QHBoxLayout*>(mpobjLO));
                              if ( pobjHBox != nullptr ) {
                                  pobjHBox->addWidget(pobjButton);
                              }
                          } else if ( strType.compare(clsXMLnode::mscszLayoutVertical) == 0 ) {
                              QVBoxLayout* pobjVBox(qobject_cast<QVBoxLayout*>(mpobjLO));
                              if ( pobjVBox != nullptr ) {
                                  pobjVBox->addWidget(pobjButton);
                              }
                          }
                      }
                  }
                  /**
                   * @brief clsQtLayout::addWidget
                   * @param pobjWidget    Pointer to widget to add
                   */
                  void clsQtLayout::addWidget(QWidget *pobjWidget) {
                      if ( pobjWidget != nullptr ) {
                          QLayout* pobjLayout(layout());
                          if ( pobjLayout != nullptr ) {
                              pobjLayout->addWidget(pobjWidget);
                          }
                      }
                  }
                  /**
                   * @brief clsQtLayout::pobjAddGroup
                   * @param crstrGroup : Group ID
                   * @return Pointer to QButtonGroup
                   */
                  QButtonGroup* clsQtLayout::pobjAddGroup(const QString& crstrGroup) {
                      QButtonGroup* pobjBtnGrp(nullptr);
                      if ( crstrGroup.isEmpty() != true ) {
                          if ( mmpGroups == nullptr ) {
                              mmpGroups = new mpGroups;
                          }
                          mpGroups::iterator itGrp(mmpGroups->find(crstrGroup));
                          if ( itGrp != mmpGroups->end() ) {
                              pobjBtnGrp = itGrp->second;
                          } else {
                              pobjBtnGrp = new QButtonGroup(this);
                              clsXMLnode::setObjName(*pobjBtnGrp, QString("%1, %2: %3")
                                                     .arg(objectName()
                                                         ,pobjBtnGrp->metaObject()->className()
                                                         ,crstrGroup));
                              mmpGroups->insert(std::make_pair(crstrGroup, pobjBtnGrp));
                          }
                  
                      }
                      return pobjBtnGrp;
                  }
                  

                  Sample of setting of the XML:

                  	<layout id="rdoLO" height="56" hspacing="0" scrollbar="false" 
                  			eol="true" type="grid"
                   			vspacing="0" properties="QLayout {background-color:#ffaaaa;}">
                  		<buttongroup id="enSEX">
                  			<radiobutton id="rdoM" height="28" text="Male" default="true"/>
                  			<radiobutton id="rdoF" height="28" text="Female"/>
                  		</buttongroup>
                  	</layout>
                  

                  And a screenshot of this:
                  Screenshot 2021-12-01 at 20.06.30.png

                  Would setting the size policy of the layout to Minimum help ?

                  Nope, tried setting size policy on containing widget and then scroller, neither worked using:

                  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
                  

                  No difference at all.

                  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