Solved Change the colors of tabs without using StyleSheets
-
-
This is using stylesheets. Stylesheets is no good for what I'm doing.
-
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;
} -
@leinad
Using new style signals/slots might give a better error. Did you include theslots
word in the header? You do not haveQ_OBJECT
word in your class, and you define a slot. Shouldn't you include it? And do a full rebuild. -
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.