网站推广的策略,软件软件开发,萍乡网站建设哪家好哦,万网做网站花多少钱无法通过QSS设置这些属性.要将样式更改为每个选项卡,我们必须创建一个自定义QTabBar并覆盖其paintEvent方法,以便能够更改我们使用QStyleOptionTab类的每个选项卡的样式,但是要更改QTabWidget选项卡,我们需要使用setTabBar方法但是这样是私有的,所以你需要创建一个自定义QTabWi… 无法通过QSS设置这些属性.要将样式更改为每个选项卡,我们必须创建一个自定义QTabBar并覆盖其paintEvent方法,以便能够更改我们使用QStyleOptionTab类的每个选项卡的样式,但是要更改QTabWidget选项卡,我们需要使用setTabBar方法但是这样是私有的,所以你需要创建一个自定义QTabWidget,如下所示 tabwidget.h #ifndef TABWIDGET_H #define TABWIDGET_H #include #include #include class TabBar: public QTabBar { public: TabBar(const QHash colors, QWidget *parent0):QTabBar(parent){ mColors colors; } protected: void paintEvent(QPaintEvent */*event*/){ QStylePainter painter(this); QStyleOptionTab opt; for(int i 0;i count();i) { initStyleOption(opt,i); if(mColors.contains(opt.text)){ opt.palette.setColor(QPalette::Button, mColors[opt.text]); } painter.drawControl(QStyle::CE_TabBarTabShape, opt); painter.drawControl(QStyle::CE_TabBarTabLabel,opt); } } private: QHash mColors; }; class TabWidget : public QTabWidget { public: TabWidget(QWidget *parent0):QTabWidget(parent){ // text - color QHash dict; dict[All] QColor(yellow); dict[purchase] QColor(#87ceeb); dict[POS Sales] QColor(#90EE90); dict[Cash Sales] QColor(pink); dict[invoice] QColor(#800080); setTabBar(new TabBar(dict)); } }; #endif // TABWIDGET_H 要在Qt Designer的QTabWidget中使用它,我们应该提升它,我们右键单击tabwidget并选择菜单Promoted Widgets,在我的例子中,前面的代码是在tabwidget.h文件中创建的,所以这将是头文件和在Promoted Class Name的情况下,我们使用TabWidget,之后我们按下Add and Promote按钮获取下图中显示的内容 最终结果如下图所示 完整示例可在以下link中找到 Python from PyQt5 import QtGui, QtWidgets class TabBar(QtWidgets.QTabBar): def __init__(self, colors, parentNone): super(TabBar, self).__init__(parent) self.mColors colors def paintEvent(self, event): painter QtWidgets.QStylePainter(self) opt QtWidgets.QStyleOptionTab() for i in range(self.count()): self.initStyleOption(opt, i) if opt.text in self.mColors: opt.palette.setColor( QtGui.QPalette.Button, self.mColors[opt.text] ) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabLabel, opt) class TabWidget(QtWidgets.QTabWidget): def __init__(self, parentNone): super(TabWidget, self).__init__(parent) d { All: QtGui.QColor(yellow), purchase: QtGui.QColor(#87ceeb), POS Sales: QtGui.QColor(#90EE90), Cash Sales: QtGui.QColor(pink), invoice: QtGui.QColor(#800080), } self.setTabBar(TabBar(d)) self.addTab(QtWidgets.QLabel(), All) self.addTab(QtWidgets.QLabel(), purchase) self.addTab(QtWidgets.QLabel(), POS Sales) self.addTab(QtWidgets.QLabel(), Cash Sales) self.addTab(QtWidgets.QLabel(), invoice) if __name__ __main__: import sys app QtWidgets.QApplication(sys.argv) app.setStyle(fusion) w TabWidget() w.show() sys.exit(app.exec_())