0
votes

So I need the user to be able to customise 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 http://doc.qt.io/archives/qt-4.8/qpalette.html#ColorRole-enum they are all the enums available for use yet none of them seem to change the background colour.

Any help will be greatly appreciated.

Cheers.

EDIT:

It seems to work when I change the tab shape to triangular, however again 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);
2
You do call repaint(). Do you?Mikhail
Should I need to? Where would I need to call it? The paintEvent function is called everytime the widget is updated, i.e. whenever I hover over it.Elliot Howard
You need to connect changing color signal to update/repaint slotMikhail
@ElliotHoward yes, in another question, they pointed out that my solution did not work and the workaround was that, it takes a style, how are you creating your style?eyllanesc

2 Answers

0
votes

As @eyllanesc suggested, I had to using a.setStyle("fusion") where 'a' is the QApplication in use.

0
votes

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