Question

I was trying to display my QTabWidget text horizontally with the Tabposition set to 2 (left side). After searching around, i found the following code from this link.

class FingerTabWidget(QtGui.QTabBar):
    def __init__(self, *args, **kwargs):
        self.tabSize = QtCore.QSize(kwargs.pop('width'), kwargs.pop('height'))
        super(FingerTabWidget, self).__init__(*args, **kwargs)

    def paintEvent(self, event):
        painter = QtGui.QStylePainter(self)
        option = QtGui.QStyleOptionTab()

        painter.begin(self)
        for index in range(self.count()):
            self.initStyleOption(option, index)
            tabRect = self.tabRect(index)
            tabRect.moveLeft(10)
            painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, option)
            painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index));
        painter.end()
    def tabSizeHint(self,index):
        return self.tabSize

I managed to apply this code by just simply using this

Tab = QtGui.QTabWidget()
Tab.setTabBar(FingerTabWidget(width=260,height=28))

Everything looks exactly like i expected and working fine but i am keep getting this error message everytime i mouse over on one of the tab.

QPainter::begin: Painter already active

Even though it does not affect the functionality of my program, i would like to know what is causing this error message. A simple explanation will do.

Was it helpful?

Solution

The calls to painter.begin() and painter.end() are redundant, as these are both done automatically when the painter is created and deleted. Re-calling painter.begin() on an active painter is technically an error, which is why you are seeing those warnings.

If you simply remove the redundant lines, the error messages should go away.

PS:

An alternate way to fix the issue would be to omit passing self to the constructor of the painter. This would leave the painter uninitialized, so the call to painter.begin(self) would then be necessary to make it active.

So, in summary, you should do either:

    painter = QtGui.QStylePainter()
    painter.begin(self)
    ...

or just:

    painter = QtGui.QStylePainter(self)
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top