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. How can I customize the title of a QGroupBox using a QProxyStyle?

How can I customize the title of a QGroupBox using a QProxyStyle?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 70 Views
  • 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.
  • L Offline
    L Offline
    l3u_
    wrote on 29 Mar 2025, 22:22 last edited by l3u_
    #1

    Hi all,

    I want to (slightly) customize the title of QGroupBoxes, but without using style sheets. Mostly to work around the problems with QSpinBoxes that emerged with Qt 6.8.2 (cf. Bug #133845).

    Until now, I set the following application-wide style sheet:

    QGroupBox[checkable=false] { font-weight: bold; }
    

    and for those I didn't want the title to be bold, I set (per group box):

    QGroupBox { font-weight: normal; }
    

    That did the trick. However, I now try to get the same result using a QProxyStyle. I added a property defaultTitle to mark those boxes who should get a non-bold title. This seemed to work fine by simply tweaking the style option and painter font. here's a minimal example:

    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QGroupBox>
    #include <QLabel>
    #include <QProxyStyle>
    #include <QStyleOptionGroupBox>
    #include <QPainter>
    #include <QDebug>
    
    class Demo : public QWidget
    {
    public:
        Demo()
        {
            auto *layout = new QVBoxLayout(this);
            QGroupBox *box;
            QVBoxLayout *boxLayout;
    
            box = new QGroupBox(tr("Custom title"));
            boxLayout = new QVBoxLayout(box);
            boxLayout->addWidget(new QLabel(tr("Some content")));
            layout->addWidget(box);
    
            box = new QGroupBox(tr("Checkable box"));
            box->setCheckable(true);
            boxLayout = new QVBoxLayout(box);
            boxLayout->addWidget(new QLabel(tr("Some content")));
            layout->addWidget(box);
    
            box = new QGroupBox(tr("Default title"));
            box->setProperty("defaultTitle", true);
            boxLayout = new QVBoxLayout(box);
            boxLayout->addWidget(new QLabel(tr("Some content")));
            layout->addWidget(box);
    
            box = new QGroupBox;
            boxLayout = new QVBoxLayout(box);
            boxLayout->addWidget(new QLabel(tr("Some content")));
            layout->addWidget(box);
        }
    };
    
    class Style : public QProxyStyle
    {
    public:
        void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option,
                                QPainter *painter, const QWidget *widget) const override
        {
            if (control == CC_GroupBox) {
                if (const auto *groupBoxOption = qstyleoption_cast<const QStyleOptionGroupBox *>(option)) {
                    if (! groupBoxOption->text.isEmpty()
                        && ! (groupBoxOption->subControls & QStyle::SC_GroupBoxCheckBox)
                        && ! widget->property("defaultTitle").toBool()) {
    
                        // Copy the option
                        QStyleOptionGroupBox opt = *groupBoxOption;
    
                        // Set a bold font
                        QFont font;
                        font.setBold(true);
                        QFontMetrics fm(font);
                        opt.fontMetrics = fm;
                        painter->setFont(font);
    
                        // Let the base style draw the box
                        QProxyStyle::drawComplexControl(control, &opt, painter, widget);
                        return;
                    }
                }
            }
    
            QProxyStyle::drawComplexControl(control, option, painter, widget);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        app.setStyle(new Style);
        Demo demo;
        demo.show();
        return app.exec();
    }
    

    This works using the Fusion style. However, it does not affect e.g. Breeze or the macOS style. I looked into Breeze, and it simply overwrites my font and font metrics settings and thus draws the default stuff. I suppose the macOS style does the same.

    I messed with this quite long. However, it seems that replacing this really minimalist style sheet is not that easy. So here's my question: Is there a straightforward way to make QGroupBox titles bold using a QProxyStyle? Or do I really have to draw the whole thing myself (calculating the rects and so on) to make this relibaly work with different base styles (Fusion, Breeze, Windows, macOS)?

    Did I miss something? Thanks for all help!

    1 Reply Last reply
    0

    1/1

    29 Mar 2025, 22:22

    • Login

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