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. Background color of a specific tab of QTabBar (QTabWidget)
Forum Updated to NodeBB v4.3 + New Features

Background color of a specific tab of QTabBar (QTabWidget)

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 9.8k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Looks like you would need to re-implement paintEvent and there you will be able to change the color of the tabs that needs it

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Goutye
      wrote on last edited by
      #6

      @SGaist Thank you again for your answer!

      That's exactly what I wanted to try. However, regarding the source code of QTabBar::paintEvent(QPaintEvent *), it seems really complicated to re-implement exactly the whole system because of the private state of QTabBarPrivate.
      So then, I will have to re-implement a large part of the whole QPaintEvent to get a result close to the original.
      I will try tomorrow to see if I can get something working, but it is my first time using a QPaintEvent at this depth, so it will take certainly a lot of time.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by
        #7

        Have you considered change text color instead?
        Another work around may be creating custom widget for such tab.
        void QTabBar::setTabButton ( int index, ButtonPosition position, QWidget * widget )

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Goutye
          wrote on last edited by
          #8

          @alex_malyu Thank you for your answer!
          My specifications required to change the color of the background, that is why I will not change the text color instead.

          Regarding the setTabButton, the problem is the button will be place on the left or right inside the tab and will not take the whole place.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Goutye
            wrote on last edited by
            #9

            I succeeded in implementing the trick. However, a new problem just appeared. A kind of gray bar is drawn under the tab on the top appeared.

            https://i.gyazo.com/55a99a8e4261252d88081c1b0bf35e25.png

            I searched a bit and find that it is due to the
            p.drawPrimitive(QStyle::PE_FrameTabBarBase, optTabBase);
            of the original paintEvent.

            The strategy I use to draw the notified tab is the following one:

            • I call the QTabBar::paintEvent(event) to have the original tab drawn. (The weird gray bar appears at this step)
            • I draw the notified tabs over the existent tabs and redraw the dragged tab.

            Is there a way to fix this weird bar? I will continue to search and post the whole code after.

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

              What OS are you running ?

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

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Goutye
                wrote on last edited by
                #11

                @SGaist Currently Windows, this bar doesn't appear on a normal QTabBar.

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

                  It could be a simple question of matching colors. Take a look at the QCommonStyle for the drawing part

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

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Goutye
                    wrote on last edited by
                    #13

                    Thanks for the answer. I was really busy until now. That doesn't seem to be related to QCommonStyle, but if it is, I do not know how to figure it out.

                    However, for the moment, I have something that's working well because we changed the designed for something more "material design".
                    Thanks for the answers!

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dariusz
                      wrote on last edited by Dariusz
                      #14

                      Hey @Goutye
                      Had the same problem recently.
                      Here is one "clear" solution that might help you... should not break anything funky!
                      It relay on using as much of native logic as possible.

                      class ColoredTabBar(QTabBar):
                          def tabColor(self, index):
                              if index in [0, 3]:
                                  return QColor(60, 90, 70)
                              return QColor(0, 0, 0, 0)
                      
                          def paintEvent(self, event):
                              painter = QStylePainter(self)
                              for index in range(self.count()):
                                  opt = QStyleOptionTab()
                                  self.initStyleOption(opt, index)
                      
                                  opt.palette.setColor(QPalette.ColorRole.Window, self.tabColor(index))
                                  painter.drawControl(QStyle.ControlElement.CE_TabBarTabShape, opt)
                                  painter.drawControl(QStyle.ControlElement.CE_TabBarTabLabel, opt)
                      

                      9 years later.... LOL

                      S 1 Reply Last reply
                      0
                      • D Dariusz

                        Hey @Goutye
                        Had the same problem recently.
                        Here is one "clear" solution that might help you... should not break anything funky!
                        It relay on using as much of native logic as possible.

                        class ColoredTabBar(QTabBar):
                            def tabColor(self, index):
                                if index in [0, 3]:
                                    return QColor(60, 90, 70)
                                return QColor(0, 0, 0, 0)
                        
                            def paintEvent(self, event):
                                painter = QStylePainter(self)
                                for index in range(self.count()):
                                    opt = QStyleOptionTab()
                                    self.initStyleOption(opt, index)
                        
                                    opt.palette.setColor(QPalette.ColorRole.Window, self.tabColor(index))
                                    painter.drawControl(QStyle.ControlElement.CE_TabBarTabShape, opt)
                                    painter.drawControl(QStyle.ControlElement.CE_TabBarTabLabel, opt)
                        

                        9 years later.... LOL

                        S Offline
                        S Offline
                        sapvi
                        wrote on last edited by
                        #15

                        @Dariusz 9 years later is just in time for me LOL

                        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