【问题标题】:Change color of QTabWidget tab's更改 QTabWidget 选项卡的颜色
【发布时间】:2014-04-30 23:53:39
【问题描述】:

我想自定义一个 QTabWidget,让每个标签都有自己的背景颜色。我知道,这不能用样式表完成,所以我将 QTabBar 子类化并将其更改为paintEvent。然后,我用自己的实现替换了 QTabWidget 的默认 QTabBar。但是,选项卡的背景颜色不会改变。有人知道我错过了什么吗?

这是一个小演示应用程序,它说明了我的问题:

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_())

亲切的问候

伯恩哈德

【问题讨论】:

    标签: colors background pyqt qtabwidget qtabbar


    【解决方案1】:

    好的,我想我得到了一些东西。这不是最漂亮的东西,但它确实有用。

    如果有人对如何解决这个问题有更好的想法......请告诉我。

    from PyQt4 import QtGui 
    from PyQt4 import QtCore
    
    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)
            painter.save()
            for index in range(self.count()): #for all tabs
    
                tabRect = self.tabRect(index)
                tabRect.adjust(-1, 3, -1, -1) #ajust size of every tab (make it smaller)
                if index == 0: #make first tab red 
                    color = QtGui.QColor(255, 0, 0)
                elif index == 1: #make second tab yellow
                    color = QtGui.QColor(255, 255, 0)
                else: #make all other tabs blue
                    color = QtGui.QColor(0, 0, 255)
                if index == self.currentIndex(): #if it's the selected tab
                    color = color.lighter(130) #highlight the selected tab with a 30% lighter color
                    tabRect.adjust(0, -3, 0, 1) #increase height of selected tab and remove bottom border
    
    
                brush = QtGui.QBrush(color)
                painter.fillRect(tabRect, brush)
    
                painter.setPen(QtGui.QPen(QtGui.QColor(QtCore.Qt.black))) #black pen (for drawing the text)
                painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter,
                                 self.tabText(index))
    
                painter.setPen(QtGui.QPen(QtGui.QColor(QtCore.Qt.gray))) #gray pen (for drawing the border)
                painter.drawRect(tabRect)
            painter.restore()
    
    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(), "Tab 1")
        tabWidget.addTab(QtGui.QWidget(), "Tab 2")
        tabWidget.addTab(QtGui.QWidget(), "Tab 3")
        tabWidget.addTab(QtGui.QWidget(), "Tab 4")
    
        tabWidget.show()
    
        sys.exit(app.exec_())
    

    【讨论】:

      【解决方案2】:

      您使用了错误的调色板角色。您需要QPalette.Window,它是通用的小部件背景颜色,而不是QPalette.Base,它用于更专业的元素,如编辑控件:

      tab.palette.setColor(QtGui.QPalette.Window, QtGui.QColor(255, 0, 0))
      

      但是,您应该注意,以这种方式绘制选项卡并不适用于所有平台。这是因为某些 Windows 和 Mac 样式在绘制选项卡时使用像素图,因此不考虑调色板的变化。有关详细信息,请参阅此Qt FAQ

      【讨论】:

      • 感谢您的回答!我尝试了几种不同的调色板角色(包括QPalette.Window),但在我的机器(Windows 7)上没有任何区别。我也考虑过使用QProxyStyle,但是这个模块似乎没有包含在PyQt中...您似乎对Py(Qt)有很多经验...您将如何解决这个问题?
      • @user2494129。恐怕我没有什么要补充的,Qt FAQ 中还没有。上次我看到这个时,Windows 上唯一可行的选择是使用样式表。但是您不能以这种方式设置单个选项卡的样式(所有选项卡的颜色都相同),而且我认为选项卡样式的其他方面可能会受到影响。所以这不是一个理想的解决方案。
      • 没问题,您已经在 Qt 常见问题解答方面帮助了我很多!我想我会尝试用 QPainter 做一些低级的绘画……我不知道它是否有效,但我认为值得一试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多