Elliot Howard
Elliot Howard

Reputation: 67

Setting the colour of individual tabs within a QTabWidget

I need the user to be able to customize the tabs completely, I.E. right click on each tab and be able to bring up a colour palette which will allow them to change the colour of that individual tab.

To do this, I've re-implemented the paintEvent function within QTabBar like many other posts have suggested, however, I just can't seem to get the tabs to actually change colour.

Currently I'm just running through each tab and changing all the QPalette properties to yellow.

The text of the tab changes to yellow BUT nothing else does!

Code for paintEvent:

protected:
void paintEvent(QPaintEvent *e) {

    QStylePainter painter(this);
    QStyleOptionTab opt;

    for (int i = 0; i < count(); i++)
    {
        initStyleOption(&opt, i);

        opt.palette.setColor(QPalette::Button, QColor("yellow"));
        opt.palette.setColor(QPalette::Base, QColor("yellow"));
        opt.palette.setColor(QPalette::Window, QColor("yellow"));
        opt.palette.setColor(QPalette::AlternateBase, QColor("yellow"));
        opt.palette.setColor(QPalette::BrightText, QColor("yellow"));
        opt.palette.setColor(QPalette::ButtonText, QColor("yellow"));
        opt.palette.setColor(QPalette::Highlight, QColor("yellow"));
        opt.palette.setColor(QPalette::Text, QColor("yellow"));
        opt.palette.setColor(QPalette::WindowText, QColor("yellow"));
        opt.palette.setColor(QPalette::Background, QColor("yellow"));
        opt.palette.setColor(QPalette::Foreground, QColor("yellow"));
        opt.palette.setColor(QPalette::ToolTipBase, QColor("yellow"));
        opt.palette.setColor(QPalette::ToolTipText, QColor("yellow"));

        painter.drawControl(QStyle::CE_TabBarTabShape, opt);
        painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
    }
}

As I said, the text changes to yellow but the background doesn't, according to QPalette::ColorRole they are all the enums available for use yet none of them seem to change the background colour.

It seems to work when I change the tab shape to triangular, however, I don't want this. Is this just a bug in Qt?

QTabWidget Props:

TabWidget* centralTab = new TabWidget();
centralTab->setTabPosition(QTabWidget::South);
centralTab->setTabShape(QTabWidget::Triangular);
centralTab->setMovable(true);
m_mainWindow->setCentralWidget(centralTab);

Upvotes: 0

Views: 425

Answers (2)

Elliot Howard
Elliot Howard

Reputation: 67

eyllanesc said:

use a.setStyle("fusion")

Where a is the QApplication in use.

Upvotes: 0

Mike
Mike

Reputation: 302

You could try : setStyleSheet( "color: rgb( yellow code );"/text color/ "background-color: rgb( color code );"/bg color/

Upvotes: -1

Related Questions