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

Setting QProxyStyle on TabBar, overrides Palette



  • Hi there..

    I wanted to change the Label Orientation in a TabBar to be Horizontal, when the TabBar is set to "West".

    This is done with this simple Style Header:

    #include <QProxyStyle>
    #include <QTabBar>
    #include <QStyleOption>
    
    class TGS_CustomTabWest : public QProxyStyle {
    public:
        QSize sizeFromContents(ContentsType type, const QStyleOption* option,
                               const QSize& size, const QWidget* widget) const {
            QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
            if (type == QStyle::CT_TabBarTab) {
                s.transpose();
            }
            return s;
        }
    
        void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const {
            if (element == CE_TabBarTabLabel) {
                if (const QStyleOptionTab* tab = qstyleoption_cast<const QStyleOptionTab*>(option)) {
                    QStyleOptionTab opt(*tab);
                    opt.shape = QTabBar::RoundedNorth;
                    QProxyStyle::drawControl(element, &opt, painter, widget);
                    return;
                }
            }
            QProxyStyle::drawControl(element, option, painter, widget);
        }
    };
    

    Problem now:
    The TabBar is drawn White with white Labels..

    Means: Setting the TabBar Style with

    ui->tabWidget_DB_Navigation->tabBar()->setStyle(new TGS_CustomTabWest);
    

    Overrides the Application Palette Settings of fusion.

    What is the correct way to re-implement the Palette for the TabBar?

    I tried doing

    ui->tabWidget_DB_Navigation->tabBar()->setPalette(darkPalette);
    

    After setting the Style.. did not work ^^

    See:
    WestTabBar.jpg

    The Tabs are just White on White.. instead of the Dark palette..

    Would be nice if you Guys can help me.


  • Lifetime Qt Champion

    Hi,

    @BDC_Patrick said in Setting QProxyStyle on TabBar, overrides Palette:

    drawControl

    Does it draw properly if you do not re-implement that method ?



  • @SGaist Yes
    The Tabs are drawn Properly, when not doing the QProxyStyle.
    But.. vertical Text..


  • Lifetime Qt Champion

    I did not meant to disable the style, just not override drawControl.



  • @SGaist But.. the rotation of the TabBar Labels is done in drawControl...
    Without it, the Labels are vertical again.. But still white Light-Fusion color Palette..

    I updated the Startpost with an Image of the problem..



  • I do still stuck on that Task..
    :(


  • Lifetime Qt Champion

    Which version of Qt are you using ?
    On which OS ?
    Are you using a specific style like fusion ?



  • @SGaist
    Current QT is 5.15.2
    OS is Windows
    Preferred Style is fusion with changing Palettes
    Language is Cpp (11+)
    IDE: CLion

    I´m currently working with the workaround, to spawn multiple buttons in a vertical Box..
    Not really beautiful..

    So.. if you know help for a TabBar.. it would be nice ^^


  • Lifetime Qt Champion

    Can you provide a complete minimal example that triggers that behavior ? That way everybody can use the same code to see the issue.



  • This post is deleted!


  • @SGaist said in Setting QProxyStyle on TabBar, overrides Palette:

    Can you provide a complete minimal example that triggers that behavior ? That way everybody can use the same code to see the issue.

    The ui file:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>test_verticalTabBar</class>
     <widget class="QWidget" name="test_verticalTabBar">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>819</width>
        <height>605</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>test_verticalTabBar</string>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0">
        <widget class="QTabWidget" name="tabWidget">
         <property name="tabPosition">
          <enum>QTabWidget::West</enum>
         </property>
         <property name="currentIndex">
          <number>0</number>
         </property>
         <widget class="QWidget" name="tab">
          <attribute name="title">
           <string>Tab 1</string>
          </attribute>
         </widget>
         <widget class="QWidget" name="tab_2">
          <attribute name="title">
           <string>Tab 2</string>
          </attribute>
         </widget>
         <widget class="QWidget" name="tab_3">
          <attribute name="title">
           <string>Seite</string>
          </attribute>
         </widget>
         <widget class="QWidget" name="tab_4">
          <attribute name="title">
           <string>Seite</string>
          </attribute>
         </widget>
         <widget class="QWidget" name="tab_5">
          <attribute name="title">
           <string>Seite</string>
          </attribute>
         </widget>
        </widget>
       </item>
      </layout>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    the Header File:

    #ifndef TEST_VERTICALTABBAR_H
    #define TEST_VERTICALTABBAR_H
    
    #include <QWidget>
    #include "tgs_customtabwest.h"
    
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class test_verticalTabBar; }
    QT_END_NAMESPACE
    
    class test_verticalTabBar : public QWidget {
    Q_OBJECT
    
    public:
        explicit test_verticalTabBar(QWidget *parent = nullptr);
    
        ~test_verticalTabBar() override;
    
    private:
        Ui::test_verticalTabBar *ui;
    };
    
    
    #endif //TEST_VERTICALTABBAR_H
    
    

    The Source File:

    #include "test_verticaltabbar.h"
    #include "ui_test_verticalTabBar.h"
    
    
    test_verticalTabBar::test_verticalTabBar(QWidget *parent) :
            QWidget(parent), ui(new Ui::test_verticalTabBar) {
        ui->setupUi(this);
    
        ui->tabWidget->tabBar()->setStyle(new TGS_CustomTabWest);
    }
    
    test_verticalTabBar::~test_verticalTabBar() {
        delete ui;
    }
    

    Style set via (in main.cpp):

    a.setStyle(QStyleFactory::create("Fusion"));
    

    My darkstyle palette (feel free to use it, too):

    void SetupDarkPalette(QPalette &palette, int highlighter, int ColorShifter){
        QColor hLColor;
        switch(highlighter){
            case 0:
                hLColor = QColor(42,130,218); //Blue
                break;
            case 1:
                hLColor = QColor(255,150,30); //Orange
                break;
            case 2:
                hLColor = QColor(145,215,0); //Green
                break;
            case 3:
                hLColor = QColor(225,225,0); //Yellow
                break;
            case 4:
                hLColor = QColor(175,90,255); //Purple
                break;
            case 5:
                hLColor = QColor(215,45,0); //Red
                break;
            case 6:
                hLColor = QColor(215,215,215); //White
                break;
            case 7:
                hLColor = QColor(115,115,115); //Grey
                break;
            case 8:
                hLColor = QColor(20,20,20); //Black
                break;
            default:
                break;
        }
    
        // modify palette to dark
        palette.setColor(QPalette::Window, QColor(53 + ColorShifter, 53 + ColorShifter, 53 + ColorShifter));
        palette.setColor(QPalette::WindowText, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127 + ColorShifter, 127 + ColorShifter, 127 + ColorShifter));
        palette.setColor(QPalette::Base, QColor(42 + ColorShifter, 42 + ColorShifter, 42 + ColorShifter));
        palette.setColor(QPalette::AlternateBase, QColor(66 + ColorShifter, 66 + ColorShifter, 66 + ColorShifter));
        palette.setColor(QPalette::ToolTipBase, Qt::white);
        palette.setColor(QPalette::ToolTipText, QColor(53 + ColorShifter, 53 + ColorShifter, 53 + ColorShifter));
        palette.setColor(QPalette::Text, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127 + ColorShifter, 127 + ColorShifter, 127 + ColorShifter));
        palette.setColor(QPalette::Dark, QColor(35 + ColorShifter, 35 + ColorShifter, 35 + ColorShifter));
        palette.setColor(QPalette::Shadow, QColor(20 + ColorShifter, 20 + ColorShifter, 20 + ColorShifter));
        palette.setColor(QPalette::Button, QColor(53 + ColorShifter, 53 + ColorShifter, 53 + ColorShifter));
        palette.setColor(QPalette::ButtonText, Qt::white);
        palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127 + ColorShifter, 127 + ColorShifter, 127 + ColorShifter));
        palette.setColor(QPalette::BrightText, Qt::red);
        palette.setColor(QPalette::Link, QColor(42 + ColorShifter, 130 + ColorShifter, 218 + ColorShifter));
        palette.setColor(QPalette::Highlight, hLColor);
        palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80 + ColorShifter, 80 + ColorShifter, 80 + ColorShifter));
        palette.setColor(QPalette::HighlightedText, QColor(255,255,255);
        palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127 + ColorShifter, 127 + ColorShifter, 127 + ColorShifter));
    
        backdropColor = QColor(53 + ColorShifter, 53 + ColorShifter, 53 + ColorShifter);
    }
    

    And the tgs_customtabwest.h:

    #ifndef TGS_CUSTOMTABWEST_H
    #define TGS_CUSTOMTABWEST_H
    
    #include <QApplication>
    #include <QStyleOptionTab>
    #include <QStylePainter>
    #include <QProxyStyle>
    #include <QTabBar>
    #include <QTabWidget>
    
    class TGS_CustomTabWest: public QProxyStyle {
    public:
        QSize sizeFromContents(ContentsType type, const QStyleOption* option,
                               const QSize& size, const QWidget* widget) const override {
            QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
            if (type == QStyle::CT_TabBarTab) {
                s.transpose();
            }
            return s;
        }
    
        void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override {
            if (element == CE_TabBarTabLabel) {
                if (const auto* tab = qstyleoption_cast<const QStyleOptionTab*>(option)) {
                    QStyleOptionTab opt(*tab);
                    opt.shape = QTabBar::RoundedNorth;
                    QProxyStyle::drawControl(element, &opt, painter, widget);
                    return;
                }
            }
            QProxyStyle::drawControl(element, option, painter, widget);
        }
    };
    
    #endif //TGS_CUSTOMTABWEST_H
    

    Calling the Widget inside my MainWindow, results in:
    Unbenannt.jpg


Log in to reply