I want to customize a QTabWidget, so that every tab has it's own background color. I know, that this cannot be done with stylesheets, so I subclassed QTabBar and changed it's paintEvent. Then, I replaced the default QTabBar of QTabWidget with my own implementation. However, the background color of the tabs doesn't change. Does anybody know what I am missing?
Here is a small demo application which illustrates my problem:
from PyQt4 import QtGui
import sys
class coloredTabBar(QtGui.QTabBar):
def __init__(self, parent = None):
QtGui.QTabBar.__init__(self, parent)
def paintEvent(self, event):
p = QtGui.QStylePainter(self)
painter = QtGui.QPainter(self)
for index in range(self.count()): #for all tabs
tab = QtGui.QStyleOptionTabV3() #create styled tab
self.initStyleOption(tab, index) #initialize with default values
#change background color to red
tab.palette.setColor(QtGui.QPalette.Base, QtGui.QColor(255, 0, 0))
p.drawControl(QtGui.QStyle.CE_TabBarTab, tab) #draw tab
class coloredTabWidget(QtGui.QTabWidget):
def __init__(self, parent = None):
QtGui.QTabWidget.__init__(self, parent)
coloredTabs = coloredTabBar()
self.setTabBar(coloredTabs) #replace default tabBar with my own implementation
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
tabWidget = coloredTabWidget()
tabWidget.addTab(QtGui.QWidget(), "Hello")
tabWidget.addTab(QtGui.QWidget(), "World")
tabWidget.show()
sys.exit(app.exec_())
Kind Regards
Bernhard