How can I customize the title of a QGroupBox using a QProxyStyle?
-
Hi all,
I want to (slightly) customize the title of
QGroupBox
es, but without using style sheets. Mostly to work around the problems withQSpinBox
es 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 propertydefaultTitle
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 aQProxyStyle
? 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!