[PyQt] Changing color of individual tabs

Maurizio Berti maurizio.berti at gmail.com
Wed Apr 11 11:20:49 BST 2018


I just posted an answer, but I'll share it here too because it might be
useful for people not on stackoverflow.

https://stackoverflow.com/a/49772477/2001654


I stumbled upon similar issues some weeks ago, and then I studied a bit
about how QStyle works. The concept is that you will let Qt draw the whole
widget, but using QStyleOption helper classes (there's almost one for every
kind of widget).[image: enter image description here]
<https://i.stack.imgur.com/5PoNU.jpg>
Here's a simple example, without stylesheets. As you can see, it has some
issues (the colored tab doesn't draw well when focused) and usually
qpalette doesn't work well with stylesheets, but at least that's a start.

I tested it and it works without consuming resources. I hope it helps.

class TabBar(QtWidgets.QTabBar):
    def __init__(self, parent):
        QtWidgets.QTabBar.__init__(self, parent)
        self.colorIndexes = parent.colorIndexes

    def paintEvent(self, event):
        qp = QtGui.QPainter(self)
        for index in range(self.count()):
            option = QtWidgets.QStyleOptionTab()
            self.initStyleOption(option, index)
            if index in self.colorIndexes:
                palette = self.palette()
                palette.setColor(palette.Window, self.colorIndexes[index])
                option.palette = palette
            self.style().drawControl(QtWidgets.QStyle.CE_TabBarTab,
option, qp, self)
class TabWidget(QtWidgets.QTabWidget):
    def __init__(self):
        QtWidgets.QTabWidget.__init__(self)
        self.colorIndexes = {
            1: QtGui.QColor(QtCore.Qt.red),
            3: QtGui.QColor(QtCore.Qt.blue),
            }
        self.setTabBar(TabBar(self))
        for i in range(5):
            w = QtWidgets.QWidget()
            self.addTab(w, 'tab {}'.format(i))


Let me know what do you think of it! :-)

Regards,
Maurizio

2018-04-10 18:16 GMT+02:00 <kristof.mulier at telenet.be>:

> Dear PyQt5 users/developers,
>
> I've been struggeling with the following problem: I can't change the color
> of an individual tab (from a QTabWidget) without losing its stylesheet or
> burning up my CPU. I've posted the question some weeks ago on StackOverflow
> - but couldn't get a meaningful answer either.
> Please take a look at the question, and win the +50 bonus points if you
> can help me out:
>
> https://stackoverflow.com/questions/49464153/giving-a-
> color-to-single-tab-consumes-too-much-processing-power-pyqt5-python-3
>
> Thank you so much.
>
> Kind greetings,
>
> Kristof Mulier
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



-- 
È 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/20180411/f802c1c0/attachment-0001.html>


More information about the PyQt mailing list