Change the colors of tabs without using StyleSheets
-
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!
-
-
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.
-
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 runningfor(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;
} -
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 runningfor(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;
} -
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 <---------------------
-
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 <---------------------
@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
-
Sorry I had a typo I meant to show this is how I define QObject in MyTabWidget.
class MyTabWidget : public QTabWidget, public QObject
{
Q_OBJECTThe error I get "direct base 'QObject' is inaccessible dur to ambiguity:
@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
-
@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.