4

This is the original Tabwidget without setting title background color

My customer ask me to do something like this; Set different background colour for title

All - Yellow
purchase - light blue
POS Sales - light green
Cash Sales - Pink
invoice - light purple

I have try the SetStyleSheet like:

     QTabBar {
          background-color : Yellow;
     }

But all tab Color changed Somebody know how to setting each QTabBar background color?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Smith Song
  • 75
  • 1
  • 5
  • 1
    Try to use different coloured icons on the tabs instead. – Simon Sep 10 '17 at 05:39
  • `ui->tableWidget->item(rowIndex,coloumIndex)->setBackgroundColor(QColor(color));` also `ui->tableWidget->item(rowIndex,coloumIndex)->setTextColor(QColor(color));` – Farhad Sep 10 '17 at 07:03

1 Answers1

5

These properties can not be set through QSS. To change the style to each tab we must create a custom QTabBar and override its paintEvent method, to be able to change the style of each tab we use the QStyleOptionTab class, but to change the QTabWidget tabbar we need to use the setTabBar method but this is private so you need to create a custom QTabWidget as shown below:

tabwidget.h

#ifndef TABWIDGET_H
#define TABWIDGET_H

#include <QStyleOptionTab>
#include <QStylePainter>
#include <QTabWidget>

class TabBar: public QTabBar
{
public:
    TabBar(const QHash<QString, QColor> &colors, QWidget *parent=0):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<QString, QColor> mColors;
};

class TabWidget : public QTabWidget
{
public:
    TabWidget(QWidget *parent=0):QTabWidget(parent){

        // text - color
        QHash <QString, QColor> 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

And to use it in our QTabWidget in Qt Designer should be promoted for this we right click on the tabwidget and select the menu Promoted Widgets, in my case the previous code is created in the file tabwidget.h so this will be the header file and in the case of Promoted Class Name we use TabWidget, after that we press the buttons Add and Promote obtaining what is shown in the following image:

enter image description here

The final result is shown in the following image:

enter image description here

The complete example can be found in the following link


Python:

from PyQt5 import QtGui, QtWidgets


class TabBar(QtWidgets.QTabBar):
    def __init__(self, colors, parent=None):
        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, parent=None):
        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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Seen, It no effect in window's system... Am I forgot Install something to Qt? i use version 5.9.1 – Smith Song Sep 15 '17 at 04:39
  • Yes, I have download your example file for trying & try to promote to my project tabwidget. Following step by step but don't have effect. – Smith Song Sep 15 '17 at 05:00
  • this is the project download from your's [link](https://github.com/SmithSong55/QTabwigetProject/tree/master/46137500-qt-tabwidget-each-tab-title-background-color) – Smith Song Sep 15 '17 at 05:25
  • This is the another project I Create for testing, still no effect [link](https://github.com/SmithSong55/QTabwigetProject/tree/master/TestingTabwidget) [image](https://github.com/SmithSong55/QTabwigetProject/blob/master/TestingTabwidget/Screenshot.png) – Smith Song Sep 15 '17 at 05:33
  • I Send My Teamviewer Id and Pass here? – Smith Song Sep 15 '17 at 05:41
  • so the solution is add a.setStyle("fusion"); to main.cpp. thanks @eyllanesc – Smith Song Sep 15 '17 at 05:56
  • Seen I still have lot to learn,Thank ya. I continue my works. – Smith Song Sep 15 '17 at 06:03