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. QTabWidget, setting colour of tab itself ?
QtWS25 Last Chance

QTabWidget, setting colour of tab itself ?

Scheduled Pinned Locked Moved Solved General and Desktop
114 Posts 4 Posters 41.8k Views
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #105

    @JoeCFD , at this point I'm out of ideas as to why it isn't working, at a guess I would say the paintEvent is not using the correct painter which means the "this" context isn't correct.

    In "my" own project I created using the UI from the project that doesn't work, I have a working set of tabs where I can set the tab colours using flags and the paintEvent which is identical to the source in the project that doesn't work.

    I've looked very carefully at everything I can and I just can't see where the issue is.

    Project that works, mainwindow.ui contains:

    centralWidget
        QFrame
            tab_widget
                overall_tab
                more tabs...
    

    The bottom of the UI file contains:

    <customwidgets>
      <customwidget>
        <class>TabWidget</class>
        <extends>QTabWidget</extends>
        <header>tabwidget.h</header>
        <container>1</container>
      </customwidget>
    </customwidgets>
    

    Project that doesn't work, has the same structure as the project that does work. Any suggestions welcome.

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @JoeCFD , at this point I'm out of ideas as to why it isn't working, at a guess I would say the paintEvent is not using the correct painter which means the "this" context isn't correct.

      In "my" own project I created using the UI from the project that doesn't work, I have a working set of tabs where I can set the tab colours using flags and the paintEvent which is identical to the source in the project that doesn't work.

      I've looked very carefully at everything I can and I just can't see where the issue is.

      Project that works, mainwindow.ui contains:

      centralWidget
          QFrame
              tab_widget
                  overall_tab
                  more tabs...
      

      The bottom of the UI file contains:

      <customwidgets>
        <customwidget>
          <class>TabWidget</class>
          <extends>QTabWidget</extends>
          <header>tabwidget.h</header>
          <container>1</container>
        </customwidget>
      </customwidgets>
      

      Project that doesn't work, has the same structure as the project that does work. Any suggestions welcome.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #106

      @SPlatten suggestion,

      • make a backup :D
      • remove the not working class entirely, cpp, h and ui file
      • replace the removed files with the ones from your working example
      • adjust the api with dummy functions so it compiles
      • see if it works as expected
      • if it does add functionality from old file back in, and regular check if it still works

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      SPlattenS 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @SPlatten suggestion,

        • make a backup :D
        • remove the not working class entirely, cpp, h and ui file
        • replace the removed files with the ones from your working example
        • adjust the api with dummy functions so it compiles
        • see if it works as expected
        • if it does add functionality from old file back in, and regular check if it still works
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #107

        @J-Hilk , thank you, that's a massive job in itself...is there any tracing I can add to the paintEvent in the tabBar to see why one works and the other doesn't....

        I'm going to compromise and try to use pre-processors to comment out sections of the CPP, rebuild and see if that works, commenting out almost everything to begin with.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by SPlatten
          #108

          @JoeCFD , @J-Hilk , its stranger and stranger, here is the paintEvent function:

          void paintEvent(QPaintEvent* pEvt) {
                  QStylePainter painter(this);
                  int intTabs(count());
                  for( int i=0; i<intTabs; i++ ) {
                      QString strTabKey(strKey(i));
                      QStyleOptionTab opt;
                      initStyleOption(&opt, i);
                      tFaultsMap::const_iterator citFound(mmpFaults.find(strTabKey));
                      if ( citFound != mmpFaults.constEnd() ) {
                          QColor color(citFound->value());
                          const int cintBlue(color.blue());
                          const int cintRed(color.red());
                          color.setBlue(cintRed);
                          color.setRed(cintBlue);
            //This only works in the project I've created not the original
                          opt.palette.setColor(QPalette::Button, color);
            //Change tab text, this works
                          opt.text = "HACK";
                      }
                      painter.drawControl(QStyle::CE_TabBarTabShape, opt);
                      painter.drawContorl(QStyle::CE_TabBarTabLabel, opt);
                  }
                  Q_UNUSED(pEvt);
             }
          

          Is there anything special I need to do in order to adjust the palette? As changing the text works.

          [Edit] In searching for a solution on Google I found a suggestion to set the Fusion style:

          QApplication::setStyle("Fusion");
          

          I added this to the TabBar constructor, rebuilt and deployed, when the form is displayed the application now crashes.

          [Edit#2] It wasn't this that caused the crash but another modification I made at the same time.

          [Edit#3] I have read online that using the palette to set the colour isn't reliable and instead to use the style sheet. I would like to try this but:

          QWidget* pobjTabButton(tabButton(i, QTabBar::LeftSide));
          

          tabButton returns NULL, this is being called in a loop in the paintEvent where the iterations of the loop is determined by the count function. Why would tabButton return NULL when there are tabs set-up ?

          Why is this so tricky? I can't get anything except NULL returned by tabButton, without the widget I cannot try setStyleSheet and the other just doesn't work (In one project at least).

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #109

            @SPlatten said in QTabWidget, setting colour of tab itself ?:

            QStyleOptionTab

            That is why I told you to check the pointers of tabs. When they are created, print their addresses to the screen. Then check their pointers here as well to make sure the same tabs are handled. Here, I mean tab buttons.

            SPlattenS 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @SPlatten said in QTabWidget, setting colour of tab itself ?:

              QStyleOptionTab

              That is why I told you to check the pointers of tabs. When they are created, print their addresses to the screen. Then check their pointers here as well to make sure the same tabs are handled. Here, I mean tab buttons.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by SPlatten
              #110

              @JoeCFD , something funky is going on tho, as I posted earlier, if I use the structure to change the tab text, this works and is clearly visible. The issue is setting the colour doesn't work so this in itself tells me the pointers must be correct.

              I was going to give using setStyleSheet a shot but I cannot get any button returned by using the function tabButton, it always returns NULL in both of the applications.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #111

                @JoeCFD , @J-Hilk , I'm comparing the UI from both projects in the form editor and looking for differences that could explain why one works and the other doesn't, so far:

                In the project that works, the UI has a setting in the tab widget, palette which is set to Cusomized (3 roles), in the project that doesn't work this is set to inherited.

                Looking at the XML for each files I cannot see anything that corresponds to this, so where is this specified?

                It doesn't make any difference, I've now got them the same and it doesn't work in the same project.

                [Edit] I've modified the paintEvent function adding more debug:

                QColor color(citFound->value());
                const int cintBlue(color.blue());
                const int cintRed(color.red());
                color.setBlue(cintRed);
                color.setRed(cintBlue);
                QColor before(opt.palette.color(QPalette::Button));
                opt.palette.setColor(QPalette::Button, color);
                QColor confirm(opt.palette.color(QPalette::Button));
                

                I also log this data to a file and I can see that the output is:

                Before: #d4d0c8, Colour:#ff0000, Confirm:#ff0000
                

                However I'm not seeing the tabs with a red background, just the standard background colour.

                [Edit#1] Looking into this as a possible solution:
                https://stackoverflow.com/questions/54070408/change-color-of-single-qtabwidget-tab

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #112

                  Finally resolved, in the end I had to replace:

                  opt.palette.setColor(QPalette::Button, color);
                  

                  with:

                  opt.palette.setColor(QPalette::Background, color);
                  

                  Its still a mystery as to why the original code worked find in one project and not in the other, both using the same release of Qt.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #113

                    Great you made it. This was a heck of a lot work. This selection is actually obsolete. But it still works. Good for you.
                    QPalette::Background Window This value is obsolete. Use Window instead.
                    4.8 is too old for anyone here.

                    SPlattenS 1 Reply Last reply
                    1
                    • JoeCFDJ JoeCFD

                      Great you made it. This was a heck of a lot work. This selection is actually obsolete. But it still works. Good for you.
                      QPalette::Background Window This value is obsolete. Use Window instead.
                      4.8 is too old for anyone here.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #114

                      @JoeCFD , thank you, will do

                      Kind Regards,
                      Sy

                      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