I'm working on a project with a GUI, for which I am using Python with PyQt4 module.
Here is my demo code:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle('PyQt4 demo')
self.setGeometry(50, 50, 1000, 1000)
self.createTabs()
self.styleTabs()
self.show()
def createTabs(self):
'''Creates a QTabWidget with 5 tabs,
named 1, 2, 3, 4, 5
'''
self.tabs = QtGui.QTabWidget(self)
self.tabs.resize(1000, 1000)
contents1 = QtGui.QWidget()
contents2 = QtGui.QWidget()
contents3 = QtGui.QWidget()
contents4 = QtGui.QWidget()
contents5 = QtGui.QWidget()
self.tabs.addTab(contents1, '1')
self.tabs.addTab(contents2, '2')
self.tabs.addTab(contents3, '3')
self.tabs.addTab(contents4, '4')
self.tabs.addTab(contents5, '5')
def styleTabs(self):
#Would like to add some code here which colors
#each tab with a different color.
pass
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
Most objects (including QtabWidget and QTabBar) support styling with CSS using .setStyleSheet(str) method. But with this I could only achieve coloring all tabs with the same color. I've also found a way to color selected, first, last tab, but could never achieve coloring a tab for ex.: with an index of 2.
For example:
self.tabs.setStyleSheet('''
QTabBar::tab {background-color: green;}
QTabBar::tab:selected {background-color: red;}
QTabBar::tab:first {background-color: red;}
QTabBar::tab:last {background-color: red;}
''')
I've also tried applying color to current QTabBar. This works with Qt, but not with PyQt apparently:
tab = self.tabs.tabBar()
tab.setStyleSheet('background-color: grey;')
The PyQt4 coloring methods didn't work either:
plt = QtGui.QPalette()
clr = QtGui.QColor()
clr.setRgb(100, 100, 100)
plt.setColor(10, clr)
tab.setPalette(plt)
I've been searching on web a lot, but haven't found any solutions for this problem. At this point, I'm not even sure an easy solution exists.
Is there a way to modify PyQt4 source code, so one of the above techniques could be applied?
Additional info:
Python version 3.4
PyQt version 4.12