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
Forum Updated to NodeBB v4.3 + New Features

Change the colors of tabs without using StyleSheets

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 748 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.
  • JoeCFDJ Offline
    JoeCFDJ Offline
    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 Offline
        JoeCFDJ Offline
        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