문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top