Important: Please read the Qt Code of Conduct -

Qt 4 QTabBar paintEvent Problem

  • Hi to all;

    I am working on a project which needs to be compiled with using version Qt 4. On this project, I am using a QTabWidget and i would like to change spesific tab background color without need of any tab widget selection I mean, I try to do this implementation without using css. I made it working on Qt 5, however when I build with Qt 4 it does not work.

    Here is my implementation of paintEvent;

    class ColorTabBar : public QTabBar
    	ColorTabBar(QWidget *parent = 0) : QTabBar(parent) { }
    	void setTabColor(int index, const QColor &color) {
    		m_colors[index] = color;
    	void paintEvent(QPaintEvent *event) {
    		QStylePainter p(this);
    		for (int i = 0; i < count(); i++) {
    			QStyleOptionTab option;
                           //I also tried QStyleOptionTabV3; 
    			QTabBar::initStyleOption(&option, i);
    			// Apply custom background color
    			if (m_colors.contains(i)) {
    				option.palette.setColor(QPalette::button, m_colors[i]);
                                    // I also tried ; option.palette.setColor(backgroundRole(), m_colors[i]); 
                                    //or QPalette::Background or QPaletteBase or any other QPalette options
    			//I tried adding also this;   p.drawControl(QStyle::CE_TabBarTab, option);
                            p.drawControl(QStyle::CE_TabBarTabLabel, option);
                            p.drawControl(QStyle::CE_TabBarTabShape, option);
    	QMap<int, QColor> m_colors;

    On version Qt4 it does not change anything, I can only change color of the text on QTabBar buttons.

    I also set setStyle on qApp;


    Do you have any idea what might be the problem ? This code works on Qt5 but not Qt4.

  • Lifetime Qt Champion


    What version of Qt are you using ?
    On what OS ?
    AFAIR, there's no fusion style for Qt 4 and styles are free to ignore palette elements.

    On a side note, Qt 4 has reached end of life a long time ago so there's likely little that can be done if it's a bug there.

  • @SGaist said in Qt 4 QTabBar paintEvent Problem:


    Hi @SGaist thanks for the reply,

    I am currently using Ubuntu 16.04 Lts which is also old version.
    I did not know that Qt4 does not have fusion style. Problem most likely is about that. I also realized that, when I start app with using sudo, it changes apps's style to really old look one and not fancy one at all and then this code works. It is now clear that, the problem is about style.

    I am gonna check if there is a way to include fusion style into Qt4.


Log in to reply