Question

Using Qt stylesheets, is it possible to set a different background colour for each tab in a QTabBar that has 4 or more tabs?

My Qt application has 6 tabs underneath the menu bar. I'd like to change their background colours to 6 different colours using stylesheets.

2 issues appear to be standing in my way:

  • I can only style the "first", "middle", and "last" tabs using pure stylesheet syntax (hence why I say "4 or more" tabs).

  • I don't think the individual tabs are child widgets of the QTabBar that I can access. The idea being that I could then attach a property to each child tab that I could reference in the stylesheet.

For example:

// Stylesheet
QTabBar::tab[index="3"] {
   background: blue;
}

// Code
QTabBar* bar = new QTabBar;
int index = bar->addTab("Tab 1");
QWidget* tab1; //= ????
tab1->setProperty("index", index);

Any help would be much appreciated. Thanks.

Was it helpful?

Solution

The individual tabs are structs rather than objects. These structs are then used on painting. See the Qt source for more details.

I've had an experiment with this and I can't find a way to access a direct index, like you indicated. For your reference, I tried using properties such as the text, toolTip, whatsThis but couldn't access any of them from the stylesheets, unfortunately.

I'm afraid I don't think what you want to do is possible without subclassing and modifying the paint events yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top