0

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);
  • You do call `repaint()`. Do you? – Bob Jul 17 '18 at 09:20
  • 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 Jul 17 '18 at 09:22
  • You need to connect changing color signal to update/repaint slot – Bob Jul 17 '18 at 09:33
  • I don't know what you mean. What changing colour signal? – Elliot Howard Jul 17 '18 at 10:00
  • At the moment I simply want all the tabs to display with a yellow background, I'll worry about the user being able to change the colours using right click later. I just want to be able to make it so the tabs are yellow. – Elliot Howard Jul 17 '18 at 10:02
  • @ElliotHoward use `a.setStyle("fusion")` – eyllanesc Jul 17 '18 at 10:09
  • 1
    Possible duplicate of [Qt TabWidget Each tab Title Background Color](https://stackoverflow.com/questions/46137500/qt-tabwidget-each-tab-title-background-color) – eyllanesc Jul 17 '18 at 10:09
  • What version of Qt are you using? – eyllanesc Jul 17 '18 at 10:10
  • What is "a" in this situation? I actually saw your answer on that thread which is where I got the inspiration to re-implement the paintEvent. 5.10.1 is the version I'm using. I've looked at your GitHub post and pretty much emulated the code, it runs through it and sets the colour but it just doesn't seem to do anything. – Elliot Howard Jul 17 '18 at 10:24
  • Ok I see what you mean with the setting the style of the entire application. This works, however it changes the look of everything and I'm working on a large project where the original style is more desirable. Is this the only way to get this to work? – Elliot Howard Jul 17 '18 at 10:55
  • 1
    @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 Jul 17 '18 at 11:05
  • Ok well I've talked it out and I think we're just going to give the fusion style a go. We were previously using the default style. The only thing that is giving me a red flag with it is the fact that the QDockWidget doesn't seem to have a grey bar on the top anymore which makes it harder to distinguish between the docked widgets above it. As well as this, the blue highlighting on the QTreeView seems a tad excessive. I'm sure there's some QT flags I can fiddle with somewhere which will change this though surely? Cheers @eyllanesc . – Elliot Howard Jul 17 '18 at 12:39

2 Answers2

0

eyllanesc said:

use a.setStyle("fusion")

Where a is the QApplication in use.

-1

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

Mike
  • 190
  • 7