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. Change the colors of tabs without using StyleSheets
QtWS25 Last Chance

Change the colors of tabs without using StyleSheets

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 629 Views
  • 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hello,

    I looked quite a bit and can seem to find a solution to this problem. I would think it would be simple to do but apparently under Qt it isn't or perhaps I'm just looking in the wrong place.

    I'd like to change the tab color for QtabWidget or QtabBar which ever makes more sense. Stylesheets doesn't seem to work because it is all or nothing and the sub-commands are insufficient to handle all the situations. Basically if an alert comes up, I want to change a tab to a specific color (not the area where the text would be after you click on the tab) just the actual tab. Once the user clicks on the tab, I want to remove the color so the user knows they already selected that tab and saw the alert. I read that one must sub-class paintEvent in QTabBar/QTabWidget but I can't seem to find any examples. Any help even a link which describes what I'm looking for would be appreciated.

    Thanks!

    1 Reply Last reply
    0
    • JoeCFDJ Online
      JoeCFDJ Online
      JoeCFD
      wrote on last edited by
      #2

      https://forum.qt.io/topic/33307/solved-qtabwidget-change-tab-s-background-color/4

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        This is using stylesheets. Stylesheets is no good for what I'm doing.

        1 Reply Last reply
        0
        • JoeCFDJ Online
          JoeCFDJ Online
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          Then create customized tab bar(setTabBar()) and tab area(addTab()). Color them separately. Their geometries might have to be handled manually as well. Could be messy. Take a detailed look at the source code of QTabWidget if it is the case. Post your problem here if you have any issues.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            leinad
            wrote on last edited by
            #5

            I think I have a work around by sub-classing paintEvent but now I'm running into this issue where I'm trying to use a signal/slot to tell me when a user clicks on a tab. I keep getting "QObject::connect: No such slot QTabWidget::processTab(int)" Can anyone explain to me why subclassing does not allow me to use signal/slot? Is there a work around?

            MyTabWidget.h:
            #ifndef MYTABWIDGET_H
            #define MYTABWIDGET_H

            #include <QApplication>
            #include <QTabWidget>
            #include <QPalette>
            #include <QTabBar>
            #include <QLabel>
            #include <QDebug>
            #include "tabbar.h"

            class MyTabWidget : public QTabWidget
            {

            TabBar *mTabBar;
            

            public:
            MyTabWidget(QWidget *parent=0);
            ~MyTabWidget();

            private slots:
            void processTab(int tabIndex);

            };

            #endif // MYTABWIDGET_H

            MytabWidget.cpp

            #include "mytabwidget.h"

            MyTabWidget::~MyTabWidget()
            {

            }

            MyTabWidget::MyTabWidget(QWidget *parent) : QTabWidget(parent)
            {
            QString text = NULL;

            mTabBar = new TabBar;
            setTabBar(mTabBar);
            

            // connect(mTabBar, SIGNAL(tabBarClicked(int)), this, SLOT(processTab(int))); //get the error described above when running
            connect(mTabBar, SIGNAL(currentChanged(int)), this, SLOT(processTab(int))); //get the error described above when running

            for(int i=0; i < 5; i++)
            {
                if(i == 4)
                    text = QString("Alert    ");
                else
                    text = QString("Tab %1    ").arg(i);
                addTab(new QLabel(text, this), text);
            //    mTabBar->setTabTextColor(i,  QColor(0, 200, 0));
            }
            

            }

            void MyTabWidget::processTab(int tabIndex)
            {
            qDebug() << tabIndex;
            }

            JonBJ 1 Reply Last reply
            0
            • L leinad

              I think I have a work around by sub-classing paintEvent but now I'm running into this issue where I'm trying to use a signal/slot to tell me when a user clicks on a tab. I keep getting "QObject::connect: No such slot QTabWidget::processTab(int)" Can anyone explain to me why subclassing does not allow me to use signal/slot? Is there a work around?

              MyTabWidget.h:
              #ifndef MYTABWIDGET_H
              #define MYTABWIDGET_H

              #include <QApplication>
              #include <QTabWidget>
              #include <QPalette>
              #include <QTabBar>
              #include <QLabel>
              #include <QDebug>
              #include "tabbar.h"

              class MyTabWidget : public QTabWidget
              {

              TabBar *mTabBar;
              

              public:
              MyTabWidget(QWidget *parent=0);
              ~MyTabWidget();

              private slots:
              void processTab(int tabIndex);

              };

              #endif // MYTABWIDGET_H

              MytabWidget.cpp

              #include "mytabwidget.h"

              MyTabWidget::~MyTabWidget()
              {

              }

              MyTabWidget::MyTabWidget(QWidget *parent) : QTabWidget(parent)
              {
              QString text = NULL;

              mTabBar = new TabBar;
              setTabBar(mTabBar);
              

              // connect(mTabBar, SIGNAL(tabBarClicked(int)), this, SLOT(processTab(int))); //get the error described above when running
              connect(mTabBar, SIGNAL(currentChanged(int)), this, SLOT(processTab(int))); //get the error described above when running

              for(int i=0; i < 5; i++)
              {
                  if(i == 4)
                      text = QString("Alert    ");
                  else
                      text = QString("Tab %1    ").arg(i);
                  addTab(new QLabel(text, this), text);
              //    mTabBar->setTabTextColor(i,  QColor(0, 200, 0));
              }
              

              }

              void MyTabWidget::processTab(int tabIndex)
              {
              qDebug() << tabIndex;
              }

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @leinad
              Using new style signals/slots might give a better error. Did you include the slots word in the header? You do not have Q_OBJECT word in your class, and you define a slot. Shouldn't you include it? And do a full rebuild.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                leinad
                wrote on last edited by VRonin
                #7

                Yes, in the header I have a private slots with the slot name. As far a Q_OBJECT, I tried placing it but I keep getting an error.

                #include <QApplication>
                #include <QTabWidget>
                #include <QPalette>
                #include <QTabBar>
                #include <QLabel>
                #include <QDebug>
                #include <QObject>
                #include "tabbar.h"
                
                class MyTabWidget : public QTabWidget , Q_OBJECT 
                {
                    Q_OBJECT    <---------------------
                    TabBar *mTabBar;
                
                public:
                    MyTabWidget(QWidget *parent=0);
                    ~MyTabWidget();
                
                
                
                private slots:
                  void processTab(int tabIndex);
                };
                

                When I add Q_OBJECT as public I get non-member function cannot have 'const' qualifier. How can I add Q_OBJECT when I declare the following?

                class MyTabWidget : public QTabWidget , Q_OBJECT   <--------------
                {
                    Q_OBJECT    <---------------------
                
                jsulmJ 1 Reply Last reply
                0
                • L leinad

                  Yes, in the header I have a private slots with the slot name. As far a Q_OBJECT, I tried placing it but I keep getting an error.

                  #include <QApplication>
                  #include <QTabWidget>
                  #include <QPalette>
                  #include <QTabBar>
                  #include <QLabel>
                  #include <QDebug>
                  #include <QObject>
                  #include "tabbar.h"
                  
                  class MyTabWidget : public QTabWidget , Q_OBJECT 
                  {
                      Q_OBJECT    <---------------------
                      TabBar *mTabBar;
                  
                  public:
                      MyTabWidget(QWidget *parent=0);
                      ~MyTabWidget();
                  
                  
                  
                  private slots:
                    void processTab(int tabIndex);
                  };
                  

                  When I add Q_OBJECT as public I get non-member function cannot have 'const' qualifier. How can I add Q_OBJECT when I declare the following?

                  class MyTabWidget : public QTabWidget , Q_OBJECT   <--------------
                  {
                      Q_OBJECT    <---------------------
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @leinad said in Change the colors of tabs without using StyleSheets:

                  class MyTabWidget : public QTabWidget , Q_OBJECT

                  What is this?!
                  Q_OBJECT is a macro which goes inside the class definition, like you already did:

                  class MyTabWidget : public QTabWidget
                  {
                      Q_OBJECT
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    leinad
                    wrote on last edited by
                    #9

                    Sorry I had a typo I meant to show this is how I define QObject in MyTabWidget.

                    class MyTabWidget : public QTabWidget, public QObject
                    {
                    Q_OBJECT

                    The error I get "direct base 'QObject' is inaccessible dur to ambiguity:

                    jsulmJ 1 Reply Last reply
                    0
                    • L leinad

                      Sorry I had a typo I meant to show this is how I define QObject in MyTabWidget.

                      class MyTabWidget : public QTabWidget, public QObject
                      {
                      Q_OBJECT

                      The error I get "direct base 'QObject' is inaccessible dur to ambiguity:

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @leinad said in Change the colors of tabs without using StyleSheets:

                      class MyTabWidget : public QTabWidget, public QObject

                      QTabWidget is already a QObject, should be

                      class MyTabWidget : public QTabWidget
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        leinad
                        wrote on last edited by
                        #11

                        @leinad said in Change the colors of tabs without using StyleSheets:

                        direct base 'QObject' is inaccessible dur to ambiguity

                        Yes, that was the original code but I still got no such slot. I got around it by using eventFilters which basically does the same thing. Thanks.

                        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