Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QButtonGroup reduce width around content?



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



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


  • Lifetime Qt Champion

    Hi,

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



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



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


  • Lifetime Qt Champion

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



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



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


Log in to reply