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

How to combine stylesheets and custom widget painting?



  • We've got a method to draw text in the QTabBar bold, because there's no provided function for it. However the bar can no longer be customised with a stylesheet, which means the entire application is themable except for the tabbar - and this really stands out!

    How can the two be merged?

    Our custom painting method for reference:

    void TStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
    {
        if (element == QStyle::CE_TabBarTab) {
            QString tabName = mpTabBar->tabData(mpTabBar->tabAt(option->rect.center())).toString();
            QFont font = widget->font();
            bool isStyleChanged = false;
            if (mBoldTabsSet.contains(tabName)||mItalicTabsSet.contains(tabName)||mUnderlineTabsSet.contains(tabName)) {
                painter->save();
                font.setBold(mBoldTabsSet.contains(tabName));
                font.setItalic(mItalicTabsSet.contains(tabName));
                font.setUnderline(mUnderlineTabsSet.contains(tabName));
                isStyleChanged = true;
                painter->setFont(font);
            }
    
            QProxyStyle::drawControl(element, option, painter, widget);
    
            if (isStyleChanged) {
                painter->restore();
            }
    
        } else {
            QProxyStyle::drawControl(element, option, painter, widget);
        }
    }
    

  • Lifetime Qt Champion

    It will not work - either use CSS or a custom Style. Afaics the stylesheet stuff is internal so you can't access it.


Log in to reply