QTabBar tab text painting?

Matic Kukovec kukovecmatic at hotmail.com
Fri Jun 4 09:26:30 BST 2021


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: [cid:09c89968-b2bd-4534-9a54-d812603119da]
The "string.h" tab is the currently selected one.

Any ideas what I'm doing wrong or missing?

Thanks,
Matic
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210604/4376e95f/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 4559 bytes
Desc: image.png
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210604/4376e95f/attachment.png>


More information about the PyQt mailing list