PDA

View Full Version : How to change color of Tab in QTabWidget?



rajesh
14th July 2006, 07:52
How to change color of Tab in QTabWidget? and how to change shape of TAB, the shape other than available in Qt.

munna
14th July 2006, 07:56
You will need to subclass QTabWidget and QTabBar. In the reimplemented version of QTabBar you can changed the color of any tab.

Use the protected function - setTabBar() of QTabWidget to set your custom tab.

wysota
16th July 2006, 09:57
If you want to change the colour of all tabs to the same value, you can avoid subclassing and just change the palette of the tab bar. To change the shape, you have to reimplement the paint event and do the drawing yourself.

rajesh
17th July 2006, 12:16
changing palette not chnaging color

see the code after subclassing
TabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFlag(false)
{
bar=tabBar();
QPalette palette;
bar->installEventFilter(this);

palette.setColor(QPalette::Active,QPalette::Button ,QColor(0,0,255));
bar->setPalette(palette);
bar->setAutoFillBackground(true);
};

see the trolltech reply:
> 1. How to change color of Tab in QTabWidget?
You can subclassing the style you are using and re-implement
drawControl() function. In this function you could fill the rectangle
that the tab covers and then call drawControl() function from the base
class.

how to do this? any idea?

wysota
17th July 2006, 12:45
changing palette not chnaging color

see the code after subclassing
TabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFlag(false)
{
bar=tabBar();
QPalette palette;
bar->installEventFilter(this);

palette.setColor(QPalette::Active,QPalette::Button ,QColor(0,0,255));
bar->setPalette(palette);
bar->setAutoFillBackground(true);
};
I would try something like:

TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent){
QPalette pal = tabBar()->palette;
pal.setColor(QPalette::Window,QColor(0,0,255));
tabBar()->setPalette(pal);
}



see the trolltech reply:
> 1. How to change color of Tab in QTabWidget?
You can subclassing the style you are using and re-implement
drawControl() function. In this function you could fill the rectangle
that the tab covers and then call drawControl() function from the base
class.

how to do this? any idea?


Subclass one of QStyle subclasses, reimplement drawControl() by changing the colour palette used for drawing the control and force that style on your application. But I think it might be easier to subclass QTabBar as already suggested.

rajesh
17th July 2006, 12:49
TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent)
{
QPalette pal = tabBar()->palette();
pal.setColor(QPalette::Window,QColor(0,0,255));
tabBar()->setPalette(pal);
}


not working

jpn
17th July 2006, 13:24
I think QWindowsXPStyle might not respect the palette.
Even setting red palette (for each and every color role) to a tab widget:


tabWidget->setPalette(QPalette(Qt::red));

Only tab bar background gets red color, see the attached screenshot.

wysota
17th July 2006, 13:55
This:

#include <QApplication>
#include <QTabWidget>
#include <QPalette>
#include <QTabBar>

class MyTabWidget : public QTabWidget {
public:
MyTabWidget(QWidget *p=0) : QTabWidget(p){
QPalette pal = tabBar()->palette();
pal.setColor(QPalette::Window, QColor(255,0,0));
pal.setColor(QPalette::Button, QColor(255,255,0));
pal.setColor(QPalette::WindowText, QColor(0,0,255));
pal.setColor(QPalette::ButtonText, QColor(255,0,255));
pal.setColor(QPalette::Base, QColor(0, 200, 0));
pal.setColor(QPalette::AlternateBase, QColor(0, 255, 255));
pal.setColor(QPalette::Text, Qt::white);
tabBar()->setPalette(pal);
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
MyTabWidget tw;
tw.addTab(new QWidget(), "Tab1");
tw.addTab(new QWidget(), "Tab2");
tw.show();
return app.exec();
}

gives the result illustrated by the attachment on Plastique. Run it with -style <stylename> to check other styles, if you want.

rajesh
18th July 2006, 09:44
I created project using qmake -tp vc xxx.pro
and compling using VC++ on windows xp.
so, hot to set -style <stylename> ?

wysota code changing only text color.

wysota
18th July 2006, 10:02
I created project using qmake -tp vc xxx.pro
and compling using VC++ on windows xp.
so, hot to set -style <stylename> ?
Run it from the command line and give it the above mentioned parameter, like:
xxx.exe -style plastique


wysota code changing only text color.
Not only (inactive tabs are coloured and active one has a yellow border), but the point is it doesn't work like expected. It probably takes the tab colour from the main widget associated with the tab, so you might try to change the colour of that and see if the tab changes too.

RohitK
21st October 2013, 07:52
It's not working for me!!!