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. Setting QProxyStyle on TabBar, overrides Palette
Forum Updated to NodeBB v4.3 + New Features

Setting QProxyStyle on TabBar, overrides Palette

Scheduled Pinned Locked Moved Unsolved General and Desktop
tabbarpalettesetstyle
12 Posts 2 Posters 1.6k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    BDC_Patrick
    wrote on 8 Nov 2021, 11:16 last edited by BDC_Patrick
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Nov 2021, 21:04 last edited by
      #2

      Hi,

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

      drawControl

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply 11 Nov 2021, 11:55
      1
      • S SGaist
        8 Nov 2021, 21:04

        Hi,

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

        drawControl

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

        B Offline
        B Offline
        BDC_Patrick
        wrote on 11 Nov 2021, 11:55 last edited by BDC_Patrick 11 Nov 2021, 11:55
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Nov 2021, 20:49 last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 14 Nov 2021, 12:33
          1
          • S SGaist
            11 Nov 2021, 20:49

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

            B Offline
            B Offline
            BDC_Patrick
            wrote on 14 Nov 2021, 12:33 last edited by BDC_Patrick
            #5

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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BDC_Patrick
              wrote on 17 Nov 2021, 12:37 last edited by
              #6

              I do still stuck on that Task..
              :(

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 12 Jan 2022, 08:23 last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                B 1 Reply Last reply 14 Jan 2022, 14:14
                0
                • S SGaist
                  12 Jan 2022, 08:23

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

                  B Offline
                  B Offline
                  BDC_Patrick
                  wrote on 14 Jan 2022, 14:14 last edited by BDC_Patrick
                  #8

                  @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 ^^

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 14 Jan 2022, 19:55 last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    B 1 Reply Last reply 19 Jan 2022, 07:51
                    1
                    • B Offline
                      B Offline
                      BDC_Patrick
                      wrote on 14 Jan 2022, 20:22 last edited by BDC_Patrick
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • S SGaist
                        14 Jan 2022, 19:55

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

                        B Offline
                        B Offline
                        BDC_Patrick
                        wrote on 19 Jan 2022, 07:51 last edited by
                        #11

                        @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

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 30 Jan 2022, 15:02 last edited by
                          #12

                          Looks likes there might be a bug that has crept in. I have a related but different issue with the macOS style.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

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