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. QTabWidget, setting colour of tab itself ?
Forum Updated to NodeBB v4.3 + New Features

QTabWidget, setting colour of tab itself ?

Scheduled Pinned Locked Moved Solved General and Desktop
114 Posts 4 Posters 61.9k Views 1 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.
  • JonBJ JonB

    @SPlatten
    Nothing in https://stackoverflow.com/questions/46137500/qt-tabwidget-each-tab-title-background-color creates multiple QTabBars or changes the QWidgets which are the content/pages of each tab to themselves be QTabBars!

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #41

    @J.Hilk, @JonB , @JoeCFD, so I've finally got everything the way it should be and in my debug output I can see that various bits are being called, but it doesn't do what it is supposed to do, I'm not seeing the tabs in a different colour.

    The modified classes:

    typedef QMap<QString, QVariant> mpFaults;
    
    class TabBar : public QTabBar {
    private:
        mpFault* Faults_;
    
    protected:
        void paintEvent(QPaintEvent* evt) {
            int intTabs(count()), intPossibleFaults(Faults_->count());
            QStylePainter painter(this);
            QStyleOptionTab opt;
     const char* cpszLog((QString("TabBar::paintEvent: %1 possibleFaults:%2\r\n")
                              .arg(intTabs).arg(intPossibleFaults)).toLatin1().data());
    TabBar::logToFile(cpszLog);
            if ( intTabs > 0 && intPossibleFaults == 0 ) {
                for ( int intTab=0; intTab<intTabs; intTab++ ) {
                    QString strText(tabText(intTab));
                    Faults_->insert(strText, QVariant(false));
                }
            }
            for( int intTab=0; intTab<intTabs; intTab++ ) {
                initStyleOption(&opt, intTab);
    const char* cpszLog((QString("TabBar::tab: %1\r\n")
                  .arg(opt.text)).toLatin1().data());
    TabBar::logToFile(cpszLog);
                mpFaults::iterator itTab(Faults_->find(opt.text));
                if ( itTab != Faults_->end() ) {
                    QVariant varValue(itTab.value());
                    bool blnState(varValue.toBool());
    const char* cpszLog((QString("TabBar::blnState: %1\r\n")
                  .arg(blnState)).toLatin1().data());
    TabBar::logToFile(cpszLog);
                    if ( blnState == true ) {
    const char* cpszLog((QString("TabBar::ColourSet: %1\r\n")
                  . arg(kErrorColour)).toLatin1().data());
                        opt.palette.setColor(QPalette::Button, QColor(kErrorColour));
                    }
                }
                painter.drawControl(QStyle::CE_TabBarTabShape, opt);
                painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
            }
        }
    public:
        static void logToFile(const char* cpszMsg) {
            FILE* fp(fopen("/usr/local/cgu/paint.txt", "at"));
            if ( fp ) {
                fputs(cpszMsg, fp);
                fclose(fp);
            }
        } 
        TabBar(QWidget* pParent = 0) : QTabBar(pParent) {
    TabBar::logToFile("TabBar::TabBar!!\r\n");
            Faults_ = new mpFaults();
        }
        bool setFault (QString& strText, bool blnState) {
            mpFaults::iterator itTab(Faults_->find(strText));
    const char* cpszLog((QString("TabBar::setFault(%1,%2)\r\n")
                    .arg(strText).arg(blnState)).toLatin1().data());
    TabBar::logToFile(cpszLog);
            if ( itTab != Faults_->end() ) {
                itTab.value() = QVariant(blnState);
                return true;
            }
            return false;
        }
    };
    
    class TabWidget : public QTabWidget {
    public:
        TabWidget(QWidget* pParent = 0) : QTabWidget(pParent) {
            setTabBar(new TabBar());
        }
        bool setFault(QString& strText, bool blnState) {
            TabBar* pTabBar(dynamic_cast<TabBar*>(tabBar()));
            if ( pTabBar ) {
                return pTabBar->setFault(strText, blnState);
            }
            return false;
        }
    };
    

    I see all the various debug messages in the log file, but tab is not showing as red which is the string literal assigned to kErrorColour:

    const char_t kErrorColour[] = "red";
    

    Logged to file:

    TabBar::paintEvent: 10 possibileFaults:10
    TabBar::tab: &Overall
    TabBar::blnState: 1
    TabBar::ColourSet: red
    

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #42

      You really want to add flashing icons to the tabs or simply change their colors?

      SPlattenS 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        You really want to add flashing icons to the tabs or simply change their colors?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #43

        @JoeCFD , changing the colour of the tab is the intention but it isn't working.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #44

          You can not change it with style sheet? My suggestion for you was to flash the icons on the tabs.

          SPlattenS 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            You can not change it with style sheet? My suggestion for you was to flash the icons on the tabs.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #45

            @JoeCFD , unfortunately not....I've been through this loop, the class I found and have linked to is supposed to work, but I can't get it to work...could it be due to Qt 4.8 ?

            Kind Regards,
            Sy

            JoeCFDJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JoeCFD , unfortunately not....I've been through this loop, the class I found and have linked to is supposed to work, but I can't get it to work...could it be due to Qt 4.8 ?

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #46

              @SPlatten Probably. 4.8 is ancient code. Nobody here will try 4.8 for you. It is amazing that the people still have patience to use it. Poor you.

              Let me check my code to see if you can use tabbar to change the color. But my code is much more customized.

              SPlattenS 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @SPlatten Probably. 4.8 is ancient code. Nobody here will try 4.8 for you. It is amazing that the people still have patience to use it. Poor you.

                Let me check my code to see if you can use tabbar to change the color. But my code is much more customized.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #47

                @JoeCFD , unfortunately its completely out of my control and the company using it won't be updating it anytime soon as there are other dependencies / reasons why they can't.

                Kind Regards,
                Sy

                JoeCFDJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @JoeCFD , unfortunately its completely out of my control and the company using it won't be updating it anytime soon as there are other dependencies / reasons why they can't.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #48

                  @SPlatten You may push them for an update and get more contracts.

                  SPlattenS 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @SPlatten You may push them for an update and get more contracts.

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #49

                    @JoeCFD , the company is a world wide provider of defence and weapon tech....they're clients are governments and the equipment is years out of date, again, I have requested an updated, but I can't do anything about it.

                    Kind Regards,
                    Sy

                    JonBJ 1 Reply Last reply
                    0
                    • JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #50
                      1. 4.8 qtabbar_cpp source code is here. copy line 1569 to 1648 to your paint event. Not sure if this will work.
                        https://dreamswork.github.io/qt4/qtabbar_8cpp_source.html
                        painting of tabs is done at line 1622.
                      2. Look at lines 1594 and 1595. These lines call initStyleOption() to create QStyleOptionTabV3 tab for each tab which is a subclass of QStyleOption
                        https://dreamswork.github.io/qt4/classQTabBar.html#a41b394d892263b6b5a0705fb979f3c8e
                        and each QStyleOption has a public palette with background color. You can change it. Try to print out the background color to see what it is.
                      3. you can find QStyleOptionTabV3 and QStyleOptionTab here
                        https://dreamswork.github.io/qt4/qstyleoption_8h_source.html
                        This way may be simpler. Good luck.
                      SPlattenS 3 Replies Last reply
                      0
                      • SPlattenS SPlatten

                        @JoeCFD , the company is a world wide provider of defence and weapon tech....they're clients are governments and the equipment is years out of date, again, I have requested an updated, but I can't do anything about it.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #51

                        @SPlatten
                        Without wanting to digress this thread. I am surprised given the age of an old 4.8 implementation they now insist they want a change in a tab colour! Can't you say "Sorry, I have looked into it, this old version won't let me do that" ?

                        SPlattenS 1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD
                          1. 4.8 qtabbar_cpp source code is here. copy line 1569 to 1648 to your paint event. Not sure if this will work.
                            https://dreamswork.github.io/qt4/qtabbar_8cpp_source.html
                            painting of tabs is done at line 1622.
                          2. Look at lines 1594 and 1595. These lines call initStyleOption() to create QStyleOptionTabV3 tab for each tab which is a subclass of QStyleOption
                            https://dreamswork.github.io/qt4/classQTabBar.html#a41b394d892263b6b5a0705fb979f3c8e
                            and each QStyleOption has a public palette with background color. You can change it. Try to print out the background color to see what it is.
                          3. you can find QStyleOptionTabV3 and QStyleOptionTab here
                            https://dreamswork.github.io/qt4/qstyleoption_8h_source.html
                            This way may be simpler. Good luck.
                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by
                          #52

                          @JoeCFD , unfortunately the company firewall will not allow access to the dreamswork domain.

                          Kind Regards,
                          Sy

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @SPlatten
                            Without wanting to digress this thread. I am surprised given the age of an old 4.8 implementation they now insist they want a change in a tab colour! Can't you say "Sorry, I have looked into it, this old version won't let me do that" ?

                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by
                            #53

                            @JonB , I was brought into this company 5 months ago and the project is very old, there is a lot of issues to resolve and this is just one of many.

                            Kind Regards,
                            Sy

                            1 Reply Last reply
                            0
                            • SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #54

                              @J.Hilk, @JonB , @JoeCFD, I tried another tack...that is calling tabButton of QTabBar to get the tab widget, I was then going to try setting the widget style sheet, however failed at the first hurdle:

                              int intTabs(count()); //This returns 10, which is the number of tabs
                              for( int intTab=0; intTab<intTabs; intTab++ ) {
                                  QString strText(tabText(intTab));  //This works and gets the tab text, e.g. &Overall
                                  QWidget* pobjTab(tabButton(intTab, QTabBar::LeftSide));
                                  if ( !pobjTab ) {
                                      pobjTab = tabButton(intTab, QTabBar::RightSide);
                                  }
                              }
                              

                              Calling tabButton with QTabBar::LeftSide or QTabBar::RightSide returns NULL. Have I done something wrong or is this something else that doesn't work in 4.8 ?

                              Kind Regards,
                              Sy

                              JonBJ 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD
                                1. 4.8 qtabbar_cpp source code is here. copy line 1569 to 1648 to your paint event. Not sure if this will work.
                                  https://dreamswork.github.io/qt4/qtabbar_8cpp_source.html
                                  painting of tabs is done at line 1622.
                                2. Look at lines 1594 and 1595. These lines call initStyleOption() to create QStyleOptionTabV3 tab for each tab which is a subclass of QStyleOption
                                  https://dreamswork.github.io/qt4/classQTabBar.html#a41b394d892263b6b5a0705fb979f3c8e
                                  and each QStyleOption has a public palette with background color. You can change it. Try to print out the background color to see what it is.
                                3. you can find QStyleOptionTabV3 and QStyleOptionTab here
                                  https://dreamswork.github.io/qt4/qstyleoption_8h_source.html
                                  This way may be simpler. Good luck.
                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by SPlatten
                                #55

                                @JoeCFD , just tried to access:
                                https://dreamworks.github.io/qt4/qtabbar_8cpp_source.html

                                on my iPhone and get 404, sorry must be my fat fingers, closed browser and tried again this time ok.

                                Kind Regards,
                                Sy

                                1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @J.Hilk, @JonB , @JoeCFD, I tried another tack...that is calling tabButton of QTabBar to get the tab widget, I was then going to try setting the widget style sheet, however failed at the first hurdle:

                                  int intTabs(count()); //This returns 10, which is the number of tabs
                                  for( int intTab=0; intTab<intTabs; intTab++ ) {
                                      QString strText(tabText(intTab));  //This works and gets the tab text, e.g. &Overall
                                      QWidget* pobjTab(tabButton(intTab, QTabBar::LeftSide));
                                      if ( !pobjTab ) {
                                          pobjTab = tabButton(intTab, QTabBar::RightSide);
                                      }
                                  }
                                  

                                  Calling tabButton with QTabBar::LeftSide or QTabBar::RightSide returns NULL. Have I done something wrong or is this something else that doesn't work in 4.8 ?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #56

                                  @SPlatten said in QTabWidget, setting colour of tab itself ?:

                                  Calling tabButton with QTabBar::LeftSide or QTabBar::RightSide returns NULL. Have I done something wrong or is this something else that doesn't work in 4.8 ?

                                  I believe either (a) the QTabBar does not have any QWidgets on/for its tabs at the time you call this or (b) it never has any QWidget on/for its tabs unless you call QTabBar::setTabButton(int index, QTabBar::ButtonPosition position, QWidget *widget).

                                  SPlattenS 1 Reply Last reply
                                  0
                                  • JoeCFDJ JoeCFD
                                    1. 4.8 qtabbar_cpp source code is here. copy line 1569 to 1648 to your paint event. Not sure if this will work.
                                      https://dreamswork.github.io/qt4/qtabbar_8cpp_source.html
                                      painting of tabs is done at line 1622.
                                    2. Look at lines 1594 and 1595. These lines call initStyleOption() to create QStyleOptionTabV3 tab for each tab which is a subclass of QStyleOption
                                      https://dreamswork.github.io/qt4/classQTabBar.html#a41b394d892263b6b5a0705fb979f3c8e
                                      and each QStyleOption has a public palette with background color. You can change it. Try to print out the background color to see what it is.
                                    3. you can find QStyleOptionTabV3 and QStyleOptionTab here
                                      https://dreamswork.github.io/qt4/qstyleoption_8h_source.html
                                      This way may be simpler. Good luck.
                                    SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #57

                                    @JoeCFD , QStyleOptionTabV2 and QStyleOptionTabV3 are according to official documentation obsolete and replaced with QStyleOptionTab.

                                    Kind Regards,
                                    Sy

                                    JoeCFDJ 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @SPlatten said in QTabWidget, setting colour of tab itself ?:

                                      Calling tabButton with QTabBar::LeftSide or QTabBar::RightSide returns NULL. Have I done something wrong or is this something else that doesn't work in 4.8 ?

                                      I believe either (a) the QTabBar does not have any QWidgets on/for its tabs at the time you call this or (b) it never has any QWidget on/for its tabs unless you call QTabBar::setTabButton(int index, QTabBar::ButtonPosition position, QWidget *widget).

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #58

                                      @JonB , (b) is the case, I was hoping that each button on the tab bar was a widget available via this call.

                                      Kind Regards,
                                      Sy

                                      JonBJ 1 Reply Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @JonB , (b) is the case, I was hoping that each button on the tab bar was a widget available via this call.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #59

                                        @SPlatten
                                        I think/suspect not. By default it does not use/place any QWidget on/for the tabs, rather something internal (just the text on the tab, no special widget).

                                        Mind you, if it helps your case (I don't know if it does) you could explicitly use QTabBar::setTabButton() to put your own widget there, then you can access that widget. I don't know whether that would aid you in your issue.

                                        You were referred to https://stackoverflow.com/questions/46137500/qt-tabwidget-each-tab-title-background-color. There in answer https://stackoverflow.com/a/46139321/489865 you are shown a picture with the tabs having their own (different) colors. Could you state clearly and unequivocally whether (a) this is what you want and (b) (if so) why you can't use that solution?

                                        SPlattenS 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @SPlatten
                                          I think/suspect not. By default it does not use/place any QWidget on/for the tabs, rather something internal (just the text on the tab, no special widget).

                                          Mind you, if it helps your case (I don't know if it does) you could explicitly use QTabBar::setTabButton() to put your own widget there, then you can access that widget. I don't know whether that would aid you in your issue.

                                          You were referred to https://stackoverflow.com/questions/46137500/qt-tabwidget-each-tab-title-background-color. There in answer https://stackoverflow.com/a/46139321/489865 you are shown a picture with the tabs having their own (different) colors. Could you state clearly and unequivocally whether (a) this is what you want and (b) (if so) why you can't use that solution?

                                          SPlattenS Offline
                                          SPlattenS Offline
                                          SPlatten
                                          wrote on last edited by
                                          #60

                                          @JonB , in my code I am now writing the geometry of each tab when each is selected to the log file and I can see the geometry is correct , I can also see that the selection of the correct colour is chosen, but is this code correct:

                                          opt.palette.setColor(QPalette::Button, QColor(kErrorColour));
                                          

                                          opt is obtained from a call to:

                                          initStyleOption(&opt, intTab);
                                          

                                          Where intTab is 0 to N and opt is defined as QStyleOptionTab.

                                          Kind Regards,
                                          Sy

                                          JonBJ 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