PyQt4: How to color each tab in QTabWidget separately?

核能气质少年 提交于 2019-12-13 03:05:02

问题


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


回答1:


Unfortunally, QTabBar doesn't expose all its properties, since its contents are not children widgets layed out in a normal fashion, but are drawn internally using private methods.

There are two possibilities, though.

  1. Draw the tabbar by hand, using its paintEvent. Using QStyle draw* methods, it is possible to customize it as you want, while keeping consistency with the current theme; that's not an easy task, but can be done.

  2. Customize the background of the current selected tab only: using QTabBar's currentChanged signal, you can easily reset the stylesheet everytime the current index is changed

This is an example:

def createTabs(self):
    #[create your tabs, then...]
    self.tabColors = {
        0: 'green', 
        1: 'red', 
        2: 'yellow', 
        3: 'orange', 
        4: 'blue', 
        }
    self.tabs.tabBar().currentChanged.connect(self.styleTabs)

[...]

def styleTabs(self, index):
    self.tabs.setStyleSheet('''
        QTabBar::tab {{}}
        QTabBar::tab:selected {{background-color: {color};}}
        '''.format(color=self.tabColors[index]))

You might want to "initialize" it when the widget is first shown, by calling styleTabs(0), since the colouring is applied when the signal is fired only.



来源:https://stackoverflow.com/questions/47844741/pyqt4-how-to-color-each-tab-in-qtabwidget-separately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!