Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 4 QTabBar paintEvent Problem
Forum Update on Tuesday, May 27th 2025

Qt 4 QTabBar paintEvent Problem

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 251 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qtross
    wrote on last edited by qtross
    #1

    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
    {
    public:
    	ColorTabBar(QWidget *parent = 0) : QTabBar(parent) { }
    
    	void setTabColor(int index, const QColor &color) {
    		m_colors[index] = color;
    	}
    
    protected:
    	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);
    		}
    	}
    
    private:
    	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;

    a.setStyle("fusion");
    

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        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.

        Q Offline
        Q Offline
        qtross
        wrote on last edited by
        #3

        @SGaist said in Qt 4 QTabBar paintEvent Problem:

        that

        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.

        Thanks

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved