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 54.9k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #79

    I have the line:

    TabWidget* pTabWidget(qobject_cast<TabWidget*>(ui->tab_widget));
    

    This results in the compiler messages for this line:

    qobject.h:378:5: instantiated from 'T qobject_cast(QObject*) [with T = cgu::vip::TabWidget*]'
    ws_status_page.cc:388:67: instantiaged from here /usr/include/qt4/QtGui/qtabwidget.h:62:5: error: void value not igored as it ought to be
    

    None of the above make any sense to me....line 388 is the line I posted.

    [Edit] Replaced line with:

    TabWidget* pTabWidget(reinterpret_cast<TabWidget*>(ui->tab_widget));
    

    Message has gone, will deploy and test.

    Kind Regards,
    Sy

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • SPlattenS SPlatten

      I have the line:

      TabWidget* pTabWidget(qobject_cast<TabWidget*>(ui->tab_widget));
      

      This results in the compiler messages for this line:

      qobject.h:378:5: instantiated from 'T qobject_cast(QObject*) [with T = cgu::vip::TabWidget*]'
      ws_status_page.cc:388:67: instantiaged from here /usr/include/qt4/QtGui/qtabwidget.h:62:5: error: void value not igored as it ought to be
      

      None of the above make any sense to me....line 388 is the line I posted.

      [Edit] Replaced line with:

      TabWidget* pTabWidget(reinterpret_cast<TabWidget*>(ui->tab_widget));
      

      Message has gone, will deploy and test.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #80

      @SPlatten
      Doesn't the qobject_cast indicate it is not a TabWidget*, and using reinterpret_cast makes it compile by lying to it?

      line 388 is the line I posted.

      You have posted hundreds of lines. Which one is 388?

      SPlattenS 1 Reply Last reply
      0
      • JonBJ JonB

        @SPlatten
        Doesn't the qobject_cast indicate it is not a TabWidget*, and using reinterpret_cast makes it compile by lying to it?

        line 388 is the line I posted.

        You have posted hundreds of lines. Which one is 388?

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

        @JonB the item I am casting from the UI is a TabWidget and the line I posted as I said in my post is line 388. From the UI file:

        <widget class="TabWidget" name="tab_widget">
        

        Kind Regards,
        Sy

        JonBJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JonB the item I am casting from the UI is a TabWidget and the line I posted as I said in my post is line 388. From the UI file:

          <widget class="TabWidget" name="tab_widget">
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #82

          @SPlatten
          Well I have never had qobject_cast issue such an error message, and never changed to reinterpret_cast. I guess you will find out whether this is an issue.

          1 Reply Last reply
          0
          • SPlattenS SPlatten

            I have the line:

            TabWidget* pTabWidget(qobject_cast<TabWidget*>(ui->tab_widget));
            

            This results in the compiler messages for this line:

            qobject.h:378:5: instantiated from 'T qobject_cast(QObject*) [with T = cgu::vip::TabWidget*]'
            ws_status_page.cc:388:67: instantiaged from here /usr/include/qt4/QtGui/qtabwidget.h:62:5: error: void value not igored as it ought to be
            

            None of the above make any sense to me....line 388 is the line I posted.

            [Edit] Replaced line with:

            TabWidget* pTabWidget(reinterpret_cast<TabWidget*>(ui->tab_widget));
            

            Message has gone, will deploy and test.

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

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

            Message has gone, will deploy and test.

            I think that error message is due to the initialisation you choose.
            Use assignment initialisation instead:

            TabWidget* pTabWidget = qobject_cast<TabWidget*>(ui->tab_widget);

            from what I can tell, the cast isn't needed at all, seems like ui->tab_widget is a TabWidget-Pointer in the first place


            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.

            JonBJ 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

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

              Message has gone, will deploy and test.

              I think that error message is due to the initialisation you choose.
              Use assignment initialisation instead:

              TabWidget* pTabWidget = qobject_cast<TabWidget*>(ui->tab_widget);

              from what I can tell, the cast isn't needed at all, seems like ui->tab_widget is a TabWidget-Pointer in the first place

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #84

              @J-Hilk said in QTabWidget, setting colour of tab itself ?:

              from what I can tell, the cast isn't needed at all, seems like ui->tab_widget is a TabWidget-Pointer in the first place

              Indeed that is the point, so reinterpret_cast should not be necessary and qobject_cast should work (or be made to work as you said).

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

                @JoeCFD , @JonB , @J-Hilk , I have created very simple test application to test my TabWidget class. I have tried to replicate the project I want to use this functionality in as much as possible, a snippet from the UI:

                <widget class="TabWidget" name="tab_widget">
                  <property name="font">
                    <font>
                      <pointsize>12</pointsize>
                      <weight>75</weight>
                      <bold>true</bold>
                    </font>
                  </property>
                  <property name="currentIndex">
                    <number>0</number>
                  </property>
                  <widget class="QWidget" name="overall_tab">
                    <property name="focusPolicy">
                      <enum>Qt::TabFocus</enum>
                    </property>
                    <attribute name="title">
                      <string>&amp;Overall</string>
                    </attribute>
                    <layout class="QGridLayout" name="gridLayout_23">
                      <item row="0" column="0">
                        <widget class="QFrame" name="overall_frame">
                

                Unfortunately I am not able to get the entire file to post as the development system is locked down. In my test project everything works find and I am able to change the colour of the tab which I do with:

                void paintEvent(QPaintEvent* pEvt) {
                    Q_UNUSED(pEvt);
                    int intTabs(count());
                    if ( intTabs == 0 ) {
                        return;
                    }
                    QStylePainter painter(this);
                    for( int intTab=0; intTab<intTabs; intTab++ ) {
                        QStyleOptionTab opt;
                        initStyleOption(&opt, intTab);
                        const char* cpszProperty(opt.text.toLatin1().data());
                        QVariant varProperty(property(cpszProperty));
                        if ( varProperty.isValid() == true ) {
                            bool blnState(varProperty.toBool());
                            if ( blnState == true ) {
                                QColor color(kErrorColour);
                                opt.palette.setColor(QPalette::Button, color);
                            }
                //Edit#1, Just add the 5 lines below, compiling and trying out
                            QWidget* pTabButton(tabButton(intTab, QTabBar::LeftSide));
                            if ( pTabButton ) {
                                pTabButton->setStyleSheet(QString("background-color: rgb(%1,%2,%3)")
                                                .arg(color.red()).arg(color.green()).arg(color.blue());
                            }
                        }
                        painter.drawControl(QStyle::CE_TabBarTabShape, opt);
                        painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
                    }
                }
                

                I can see that the loop is paintEvent is being called and it iterates through the loop with the colour being set, but the colour is not displayed.

                The test application works perfectly, however the same code in the actual project does not.

                Is there anything I can do to find out why it isn't working?

                [Edit#2] Edit#1 didn't resolve anything, still doesn't work.

                Kind Regards,
                Sy

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

                  @JoeCFD , @JonB , @J-Hilk Still working on this...the current paintEvent function:

                  void paintEvent(QPaintEvent* pEvt) {
                      Q_UNUSED(pEvt);
                      int intTabs(count());
                      QStylePainter painter(this);
                      for( int intTab=0; intTab<intTabs; intTab++ ) {
                          QStyleOptionTab opt;
                          initStyleOption(&opt, intTab);
                          const char* cpszProperty(opt.text.toLatin1().data());
                          QVariant varProperty(property(cpszProperty));
                          bool blnValid(varProperty.isValid());
                          if ( blnValid == true ) {
                              bool blnState(varProperty.toBool());
                              if ( blnState == true ) {
                                  QColor color(kErrorColour);
                                  if ( color.isValid() ) {
                                      opt.palette.setColor(QPalette::Button, color);
                                  }
                              }
                          }
                          painter.drawControl(QStyle::CE_TabBarTabShape, opt);
                          painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
                      }
                  }
                  

                  What I am seeing is that blnValid is always false and the text member in opt never matches any of the tabs. Why could this be?

                  In my test application the problem does not exist, so it has to be some I have failed to do.

                  Kind Regards,
                  Sy

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

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

                                          • Login

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