PyQt4:如何分别为QTabWidget中的每个选项卡着色? [英] PyQt4: How to color each tab in QTabWidget separately?
问题描述
我正在使用GUI进行项目,为此我将Python与PyQt4模块配合使用.
I'm working on a project with a GUI, for which I am using Python with PyQt4 module.
这是我的演示代码:
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()
大多数对象(包括QtabWidget和QTabBar)都支持使用.setStyleSheet(str)方法的CSS样式.但是,我只能用相同的颜色为所有标签着色.我还找到了一种为所选的第一个,最后一个标签着色的方法,但是永远无法实现为ex .:的标签着色,索引为2.
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.
例如:
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;}
''')
我还尝试将颜色应用于当前的QTabBar.这适用于Qt,但显然不适用于PyQt:
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;')
PyQt4着色方法也不起作用:
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.
是否有一种方法可以修改PyQt4源代码,因此可以应用上述技术之一?
Is there a way to modify PyQt4 source code, so one of the above techniques could be applied?
其他信息:
-
Python 3.4版
Python version 3.4
PyQt版本4.12
PyQt version 4.12
推荐答案
不幸的是,QTabBar并未公开其所有属性,因为它的内容不是按常规方式布局的子级小部件,而是使用私有方法在内部绘制的.
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.
但是有两种可能性.
-
使用其paintEvent手动绘制选项卡.使用QStyle draw *方法,可以根据需要自定义它,同时保持与当前主题的一致性.这不是一件容易的事,但可以完成.
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.
仅自定义当前选定选项卡的背景:使用QTabBar的currentChanged
信号,您可以在每次更改当前索引时轻松地重置样式表
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
这是一个示例:
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]))
您可能希望在第一次显示窗口小部件时通过调用styleTabs(0)对其进行初始化",因为仅在信号触发时才应用着色.
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.
这篇关于PyQt4:如何分别为QTabWidget中的每个选项卡着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!