QTabBar tab text painting?

Maurizio Berti maurizio.berti at gmail.com
Fri Jun 4 14:46:23 BST 2021


Hello Matic,

The whole resetting the palette in the for loop is completely unnecessary
(especially if done every time in a for loop, you can create a palette the
first time, and then overwrite opt.palette, but that would be unnecessary
anyway since the palette should not change between tabs).
Also, the painter save/restore is completely pointless like that: the point
of saving the state of the painter is that you can temporarily change
painter settings and restore the previous state (which should also match
the state level: it's not really clear since the formatting is not
monospaced, but it seems that you're calling painter.restore() in the for
loop, and if that's the case, you should not, as you'd have
unmatched levels of saved states). Since you're not painting anything
before or after that, there's no benefit.

Anyway, the main issue here is that you're probably using Windows, and the
Windows QStyle does paint some things a bit differently, including possibly
ignoring the palette of the style option (but, hey, that could also be a
bug, unfortunately I cannot test it).

That said, if you only want to change the color of the tab text,
subclassing and overriding the paintEvent is a bit too much (and also
risky: your implementation doesn't take into account movable tabs, which is
really tricky). I'd suggest a more simpler:

self.tabWidget.setStyleSheet('QTabBar { color: red; }')

Which should solve the whole problem on any platform.

Be aware that it's very important that you use the class selector (unless
you set the stylesheet on the tab bar), if you use generic/universal
properties you'll get unexpected results, especially for a container widget
such a QTabWidget.
So, *never* do the following: it works, but it works very badly.

self.tabWidget.setStyleSheet('color: red') # NO!


Maurizio

PS: it took me a while to understand what "data" actually was; I'd strongly
suggest you against that choice, "data" is a very generic word and doesn't
reflect what it refers to in your code (and it's also very confusing to
others reading your code): what happens whenever you have a function that
*actually* uses some "data"? Why should you then choose another word
instead of an obvious name, because you already used that name for a wrong
reference and you cannot overwrite it because it's required in the function
scope?

Il giorno ven 4 giu 2021 alle ore 10:26 Matic Kukovec <
kukovecmatic at hotmail.com> ha scritto:

> Hi guys,
>
> I have a subclassed QTabBar which I want to manually change the color of
> each tab.
> After a lot of trial-and-error, I tried this to make all the tabs red:
>
> *def paintEvent(self, event):*
> *            painter = data.QStylePainter(self)*
> *            opt = data.QStyleOptionTab()*
> *            painter.save()*
>
>
> *            for i in range(self.count()):*
> *                self.initStyleOption(opt, i)*
> *                items = (*
> *                    data.QPalette.Window,*
> *                    data.QPalette.Background,*
> *                    data.QPalette.WindowText,*
> *                    data.QPalette.Foreground,*
> *                    data.QPalette.Base,*
> *                    data.QPalette.AlternateBase,*
> *                    data.QPalette.ToolTipBase,*
> *                    data.QPalette.ToolTipText,*
> *                    data.QPalette.PlaceholderText,*
> *                    data.QPalette.Text,*
> *                    data.QPalette.Button,*
> *                    data.QPalette.ButtonText,*
> *                    data.QPalette.BrightText,*
> *                    data.QPalette.Light,*
> *                    data.QPalette.Midlight,*
> *                    data.QPalette.Dark,*
> *                    data.QPalette.Mid,*
> *                    data.QPalette.Shadow,*
> *                    data.QPalette.Highlight,*
> *                    data.QPalette.HighlightedText,*
> *                    data.QPalette.Link,*
> *                    data.QPalette.LinkVisited,*
> *                )*
> *                groups = (*
> *                    data.QPalette.Disabled,*
> *                    data.QPalette.Active,*
> *                    data.QPalette.Inactive,*
> *                    data.QPalette.Normal,*
> *                )*
> *                color = data.QColor(0xffff0000)*
> *                for g in groups:*
> *                    for p in items:*
> *                        opt.palette.setColor(g, p, color)*
> *                        opt.palette.setBrush(g, p, color)*
> *                painter.setBrush(data.QBrush(data.QColor(color)))*
> *                painter.setPen(data.QPen(data.QColor(color)))*
> *                painter.drawControl(data.QStyle.CE_TabBarTabLabel, opt)*
>
>
> *                           painter.restore()*
>
> Now this works, but only for the non-currently selected tabs, the selected
> tab ALWAYS has black text and I have no idea why.
> Here is a screenshot:
> The "string.h" tab is the currently selected one.
>
> Any ideas what I'm doing wrong or missing?
>
> Thanks,
> Matic
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210604/3a0353ba/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 4559 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210604/3a0353ba/attachment.png>


More information about the PyQt mailing list