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 ?
Forum Updated to NodeBB v4.3 + New Features

QTabWidget, setting colour of tab itself ?

Scheduled Pinned Locked Moved Solved General and Desktop
114 Posts 4 Posters 45.2k 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.
  • JoeCFDJ JoeCFD

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

    QStyleOptionTab

    opt.text is a qstring. Print it to the screen or use debugger to check its value. Then you can see if it is valid or not.

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

    @JoeCFD , @JonB , @J-Hilk , I've progressed this to the point where it gets all the way to:

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

    I output the color to the log file and its valid and the name() returns #ff0000. But still the colour of the tab doesn't change, again in my standard alone test project it does the same and in this it works...so why doesn't it work here?

    Kind Regards,
    Sy

    JoeCFDJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @JoeCFD , @JonB , @J-Hilk , I've progressed this to the point where it gets all the way to:

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

      I output the color to the log file and its valid and the name() returns #ff0000. But still the colour of the tab doesn't change, again in my standard alone test project it does the same and in this it works...so why doesn't it work here?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #89

      @SPlatten Make sure the same tabs are painted. Assign object names to the tabs when they are created. Check if the names are the same when they are painted.

      SPlattenS 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @SPlatten Make sure the same tabs are painted. Assign object names to the tabs when they are created. Check if the names are the same when they are painted.

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

        @JoeCFD , I can see in the ui / XML file that each widget / tab has a name attribute. How do I go about checking if the names are the same when painted?

        Kind Regards,
        Sy

        JoeCFDJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JoeCFD , I can see in the ui / XML file that each widget / tab has a name attribute. How do I go about checking if the names are the same when painted?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #91

          @SPlatten it is object name. https://doc.qt.io/qt-5/qobject.html#objectName-prop

          SPlattenS 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @SPlatten it is object name. https://doc.qt.io/qt-5/qobject.html#objectName-prop

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

            @JoeCFD , thank you, I realise that but I was expecting the QStyleOptionTab to have some member that represents the tab object name, it doesn't, the only way I can think of doing this is to make the object name the same as the tab text ?

            Kind Regards,
            Sy

            JoeCFDJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JoeCFD , thank you, I realise that but I was expecting the QStyleOptionTab to have some member that represents the tab object name, it doesn't, the only way I can think of doing this is to make the object name the same as the tab text ?

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #93

              @SPlatten The better way to check it is to make sure the pointer addresses of your tabs are the same as the ones when they are created. Changing object names can not help anything. It is only one way for debug.

              SPlattenS 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @SPlatten The better way to check it is to make sure the pointer addresses of your tabs are the same as the ones when they are created. Changing object names can not help anything. It is only one way for debug.

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

                @JoeCFD , I'm not sure how this is going to help as only one set of tabs is displayed. I could try changing the text of any tab to see if that updates...if it does it doesn't explain why the colour doesn't change.

                Kind Regards,
                Sy

                JoeCFDJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @JoeCFD , I'm not sure how this is going to help as only one set of tabs is displayed. I could try changing the text of any tab to see if that updates...if it does it doesn't explain why the colour doesn't change.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #95

                  @SPlatten Good idea. Try to change the texts to see if it works.

                  SPlattenS 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @SPlatten Good idea. Try to change the texts to see if it works.

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

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

                    @SPlatten Good idea. Try to change the texts to see if it works.

                    I've modified the code where it sets the colour to also change the tab text:

                    setTabText(intTab, "modified: " + tabText(intTab));
                    

                    [Edit] With this code in place I can see that no tab text is modified, which makes me wonder what is going on, the tabs show the same as defined in the UI file, but it appears they aren't the same in that they cannot be modified???

                    Kind Regards,
                    Sy

                    SPlattenS 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

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

                      @SPlatten Good idea. Try to change the texts to see if it works.

                      I've modified the code where it sets the colour to also change the tab text:

                      setTabText(intTab, "modified: " + tabText(intTab));
                      

                      [Edit] With this code in place I can see that no tab text is modified, which makes me wonder what is going on, the tabs show the same as defined in the UI file, but it appears they aren't the same in that they cannot be modified???

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

                      @JoeCFD , so now I'm looking at why this is the case and what I can do to fix it.

                      [Edit] I removed this:

                      <header location="global">ws_status_page.h</header>
                      

                      From the customWidget defining the class TabWidget and now the modified text is visible, although when I applied the modified text to the project that the colours work in, the colours don't work while the text is modified, so I'm now rebuilding and testing without modified text...

                      [Edit#2] Sadly having removed the code that modified the text the colour still isn't set. But this does still work in the other project when I removed the modified text.

                      Kind Regards,
                      Sy

                      SPlattenS 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @JoeCFD , so now I'm looking at why this is the case and what I can do to fix it.

                        [Edit] I removed this:

                        <header location="global">ws_status_page.h</header>
                        

                        From the customWidget defining the class TabWidget and now the modified text is visible, although when I applied the modified text to the project that the colours work in, the colours don't work while the text is modified, so I'm now rebuilding and testing without modified text...

                        [Edit#2] Sadly having removed the code that modified the text the colour still isn't set. But this does still work in the other project when I removed the modified text.

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

                        @JonB , @JoeCFD , @J-Hilk I'm still working on this and trying to find a solution, in the test project I created I've added to the end the TabBar::paintEvent function:

                        QPen objPen(QColor("#ffff00"));
                        objPen.setWidth(3);
                        painter.setPen(objPen);
                        painter.drawLine(0,0,100,100);
                        

                        Having cleaned and rebuilt both projects I can see the thick yellow diagonal line drawn in both projects, but only the prototype I created has the tabs drawn with a red background.

                        Kind Regards,
                        Sy

                        JoeCFDJ 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @JonB , @JoeCFD , @J-Hilk I'm still working on this and trying to find a solution, in the test project I created I've added to the end the TabBar::paintEvent function:

                          QPen objPen(QColor("#ffff00"));
                          objPen.setWidth(3);
                          painter.setPen(objPen);
                          painter.drawLine(0,0,100,100);
                          

                          Having cleaned and rebuilt both projects I can see the thick yellow diagonal line drawn in both projects, but only the prototype I created has the tabs drawn with a red background.

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by JoeCFD
                          #99

                          @SPlatten I still suspect that not the right tab is called. You do need to check their pointer addresses match exactly. Try to make only one tab in your project for testing. If this is confirmed, you need to dig into the source code to check what can block the change of color. There must be some flags for this.

                          SPlattenS 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @SPlatten I still suspect that not the right tab is called. You do need to check their pointer addresses match exactly. Try to make only one tab in your project for testing. If this is confirmed, you need to dig into the source code to check what can block the change of color. There must be some flags for this.

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

                            @JoeCFD , thanks for the reply, I have the same login for the paintEvent in both applications, so the check and code is the same, one works (my sample), the other doesn't, how would you like me to change the paintEvent function?

                            Kind Regards,
                            Sy

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

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

                              initStyleOption

                              Can you make a debug build of Qt? Then build your project in debug mode and you can easily check into the source code to see what is going on? There must some tiny change which is missing. You are so close now.

                              SPlattenS 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

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

                                initStyleOption

                                Can you make a debug build of Qt? Then build your project in debug mode and you can easily check into the source code to see what is going on? There must some tiny change which is missing. You are so close now.

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

                                @JoeCFD , unfortunately thats much easier said than done, the entire project is huge and not built with Qt, instead many many cmake's.

                                Kind Regards,
                                Sy

                                JoeCFDJ 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @JoeCFD , unfortunately thats much easier said than done, the entire project is huge and not built with Qt, instead many many cmake's.

                                  JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #103

                                  @SPlatten you need only qt debug build. Do not worry about other packages.

                                  SPlattenS 1 Reply Last reply
                                  0
                                  • JoeCFDJ JoeCFD

                                    @SPlatten you need only qt debug build. Do not worry about other packages.

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

                                    @JoeCFD , I wish, I should have stated that the target system does not have Qt Creator on it.

                                    Kind Regards,
                                    Sy

                                    1 Reply Last reply
                                    0
                                    • 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

                                          • Login

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