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. QHeaderView induvidual background colors
Forum Updated to NodeBB v4.3 + New Features

QHeaderView induvidual background colors

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.4k 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.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by Andy314
    #1

    Hello, I need a QTableView with induvidual dynamic Header Coloring.
    Stylesheeds dont help because they syle all colums with the same style.

    I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.

    What can I do ?

    Is there a methode with rules for the header ?

    Pl45m4P 1 Reply Last reply
    0
    • Andy314A Andy314

      Hello, I need a QTableView with induvidual dynamic Header Coloring.
      Stylesheeds dont help because they syle all colums with the same style.

      I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.

      What can I do ?

      Is there a methode with rules for the header ?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @Andy314 said in QHeaderView induvidual background colors:

      I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.

      You can re-implement paintSection, add your things you want to change and then call the base implementation QHeaderView::paintSection to preserve the default look where you do not make changes.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Andy314A 1 Reply Last reply
      2
      • Pl45m4P Pl45m4

        @Andy314 said in QHeaderView induvidual background colors:

        I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.

        You can re-implement paintSection, add your things you want to change and then call the base implementation QHeaderView::paintSection to preserve the default look where you do not make changes.

        Andy314A Offline
        Andy314A Offline
        Andy314
        wrote on last edited by
        #3

        @Pl45m4 said in QHeaderView induvidual background colors:

        @Andy314 said in QHeaderView induvidual background colors:

        I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.

        You can re-implement paintSection, add your things you want to change and then call the base implementation QHeaderView::paintSection to preserve the default look where you do not make changes.

        This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.

        Pl45m4P 1 Reply Last reply
        1
        • Andy314A Andy314

          @Pl45m4 said in QHeaderView induvidual background colors:

          @Andy314 said in QHeaderView induvidual background colors:

          I tried to derive my own QHeaderview and want overwrite the paintSection() function. Simple drawing actions I can develop, but the original code is much more complicated and sohisticated. I want this functionality with a little bit of manipulation. Best way would be to copy the code in my class, but I can compile it because of its private member access.

          You can re-implement paintSection, add your things you want to change and then call the base implementation QHeaderView::paintSection to preserve the default look where you do not make changes.

          This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @Andy314 said in QHeaderView induvidual background colors:

          This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.

          These two are your friends :)

          • https://doc.qt.io/qt-6/qpainter.html#save
          • https://doc.qt.io/qt-6/qpainter.html#restore

          Save the current painter, call the base impl., restore the painter and paint your stuff on top of it


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          Andy314A 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Andy314 said in QHeaderView induvidual background colors:

            This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.

            These two are your friends :)

            • https://doc.qt.io/qt-6/qpainter.html#save
            • https://doc.qt.io/qt-6/qpainter.html#restore

            Save the current painter, call the base impl., restore the painter and paint your stuff on top of it

            Andy314A Offline
            Andy314A Offline
            Andy314
            wrote on last edited by Andy314
            #5

            @Pl45m4 said in QHeaderView induvidual background colors:

            @Andy314 said in QHeaderView induvidual background colors:

            This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.

            These two are your friends :)

            • https://doc.qt.io/qt-6/qpainter.html#save
            • https://doc.qt.io/qt-6/qpainter.html#restore

            Save the current painter, call the base impl., restore the painter and paint your stuff on top of it

            Ok, this should work, for add additdional things over the standard draw - for background color not.
            Mybe I should take the whole original code an name the class to a new name and break the private members.

            A problem in this context while create a custom QHeaderView I found. I made my own QTableView with a custom QHeaderView.

            class AdvHeaderView : public QHeaderView;
            
            AdvTblView::AdvTblView(QWidget *parent): QTableView(parent)
              {
                HeaderView= new AdvHeaderView(Qt::Horizontal, this);
                setHorizontalHeader(HeaderView);
                QObject::connect(HeaderView,
                                 &QHeaderView::sectionClicked,
                                 this,
                                 &AdvTblView::headerClicked);
            

            The sectionClicked event does not fire for the custom HeaderView. For the standard HeaderView all works.

            Pl45m4P 1 Reply Last reply
            0
            • Andy314A Andy314

              @Pl45m4 said in QHeaderView induvidual background colors:

              @Andy314 said in QHeaderView induvidual background colors:

              This does not work, because the QHeaderView::paintSection makes the complete drawing and overwrites all, especially the backgouround.

              These two are your friends :)

              • https://doc.qt.io/qt-6/qpainter.html#save
              • https://doc.qt.io/qt-6/qpainter.html#restore

              Save the current painter, call the base impl., restore the painter and paint your stuff on top of it

              Ok, this should work, for add additdional things over the standard draw - for background color not.
              Mybe I should take the whole original code an name the class to a new name and break the private members.

              A problem in this context while create a custom QHeaderView I found. I made my own QTableView with a custom QHeaderView.

              class AdvHeaderView : public QHeaderView;
              
              AdvTblView::AdvTblView(QWidget *parent): QTableView(parent)
                {
                  HeaderView= new AdvHeaderView(Qt::Horizontal, this);
                  setHorizontalHeader(HeaderView);
                  QObject::connect(HeaderView,
                                   &QHeaderView::sectionClicked,
                                   this,
                                   &AdvTblView::headerClicked);
              

              The sectionClicked event does not fire for the custom HeaderView. For the standard HeaderView all works.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @Andy314 said in QHeaderView induvidual background colors:

              QObject::connect(HeaderView,
              &QHeaderView::sectionClicked,
              this,
              &AdvTblView::headerClicked);

              Try &AdvHeaderView::sectionClicked not QHeaderView since your HeaderView obj is from type AdvHeaderView.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              Andy314A 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @Andy314 said in QHeaderView induvidual background colors:

                QObject::connect(HeaderView,
                &QHeaderView::sectionClicked,
                this,
                &AdvTblView::headerClicked);

                Try &AdvHeaderView::sectionClicked not QHeaderView since your HeaderView obj is from type AdvHeaderView.

                Andy314A Offline
                Andy314A Offline
                Andy314
                wrote on last edited by
                #7

                @Pl45m4 said in QHeaderView induvidual background colors:

                @Andy314 said in QHeaderView induvidual background colors:

                QObject::connect(HeaderView,
                &QHeaderView::sectionClicked,
                this,
                &AdvTblView::headerClicked);

                Try &AdvHeaderView::sectionClicked not QHeaderView since your HeaderView obj is from type AdvHeaderView.

                No, nothing works.
                I have moved the code outside of the constructor for simplification. All 3 methodes gave no successd.

                QObject::connect(
                          ui->TV->horizontalHeader(),
                          SIGNAL(sectionClicked(int)),
                          this,
                          SLOT(headerClicked(int)));
                QObject::connect(
                        ui->TV->horizontalHeader(),
                        &QHeaderView::sectionClicked,
                        this,
                        &VoyList_Frame::headerClicked);
                QObject::connect(
                        ui->TV->horizontalHeader(),
                        &AdvHeaderView::sectionClicked,
                        this,
                        &VoyList_Frame::headerClicked);
                
                
                JonBJ 1 Reply Last reply
                0
                • Andy314A Andy314

                  @Pl45m4 said in QHeaderView induvidual background colors:

                  @Andy314 said in QHeaderView induvidual background colors:

                  QObject::connect(HeaderView,
                  &QHeaderView::sectionClicked,
                  this,
                  &AdvTblView::headerClicked);

                  Try &AdvHeaderView::sectionClicked not QHeaderView since your HeaderView obj is from type AdvHeaderView.

                  No, nothing works.
                  I have moved the code outside of the constructor for simplification. All 3 methodes gave no successd.

                  QObject::connect(
                            ui->TV->horizontalHeader(),
                            SIGNAL(sectionClicked(int)),
                            this,
                            SLOT(headerClicked(int)));
                  QObject::connect(
                          ui->TV->horizontalHeader(),
                          &QHeaderView::sectionClicked,
                          this,
                          &VoyList_Frame::headerClicked);
                  QObject::connect(
                          ui->TV->horizontalHeader(),
                          &AdvHeaderView::sectionClicked,
                          this,
                          &VoyList_Frame::headerClicked);
                  
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Andy314

                  class AdvHeaderView : public QHeaderView;
                  

                  You are finding that your derived AdvHeaderView is not producing any signals, is that right? Did you put Q_OBJECT macro in your declaration/definition of AdvHeaderView? Not sure, that may be required here?

                  Andy314A 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Andy314

                    class AdvHeaderView : public QHeaderView;
                    

                    You are finding that your derived AdvHeaderView is not producing any signals, is that right? Did you put Q_OBJECT macro in your declaration/definition of AdvHeaderView? Not sure, that may be required here?

                    Andy314A Offline
                    Andy314A Offline
                    Andy314
                    wrote on last edited by
                    #9

                    @JonB said in QHeaderView induvidual background colors:

                    @Andy314

                    class AdvHeaderView : public QHeaderView;
                    

                    You are finding that your derived AdvHeaderView is not producing any signals, is that right? Did you put Q_OBJECT macro in your declaration/definition of AdvHeaderView? Not sure, that may be required here?

                    I seen that it produces no signals. Unfortunately Q_OBJECT wasn't the problem

                    class AdvHeaderView : public QHeaderView
                    {
                        Q_OBJECT
                    public:
                        AdvHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
                            : QHeaderView(orientation, parent) {}
                    protected:
                        void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
                    }
                    
                    JonBJ 1 Reply Last reply
                    0
                    • Andy314A Andy314

                      @JonB said in QHeaderView induvidual background colors:

                      @Andy314

                      class AdvHeaderView : public QHeaderView;
                      

                      You are finding that your derived AdvHeaderView is not producing any signals, is that right? Did you put Q_OBJECT macro in your declaration/definition of AdvHeaderView? Not sure, that may be required here?

                      I seen that it produces no signals. Unfortunately Q_OBJECT wasn't the problem

                      class AdvHeaderView : public QHeaderView
                      {
                          Q_OBJECT
                      public:
                          AdvHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
                              : QHeaderView(orientation, parent) {}
                      protected:
                          void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Andy314

                      • Temporarily replace your AdvHeaderView with a plain QHeaderView. Does the signal work?
                      • Is it just the sectionClicked() signal which does not work or all signals do not work?
                      • Make sure your headerClicked() has a qDebug() so we are sure it not getting called and/or try a lambda with just qDebug()in it as the slot.
                      Andy314A 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Andy314

                        • Temporarily replace your AdvHeaderView with a plain QHeaderView. Does the signal work?
                        • Is it just the sectionClicked() signal which does not work or all signals do not work?
                        • Make sure your headerClicked() has a qDebug() so we are sure it not getting called and/or try a lambda with just qDebug()in it as the slot.
                        Andy314A Offline
                        Andy314A Offline
                        Andy314
                        wrote on last edited by
                        #11

                        @JonB said in QHeaderView induvidual background colors:

                        @Andy314

                        • Temporarily replace your AdvHeaderView with a plain QHeaderView. Does the signal work?
                        • Is it just the sectionClicked() signal which does not work or all signals do not work?
                        • Make sure your headerClicked() has a qDebug() so we are sure it not getting called and/or try a lambda with just qDebug()in it as the slot.

                        If I reset the integrated HeaderView all works.
                        I had integrated debug messages.

                        Signal SectionResized and GeomtryChaged work always. All Mouse Enter/Click/DoubleClick etc. dont work.

                        JonBJ 1 Reply Last reply
                        1
                        • Andy314A Andy314

                          @JonB said in QHeaderView induvidual background colors:

                          @Andy314

                          • Temporarily replace your AdvHeaderView with a plain QHeaderView. Does the signal work?
                          • Is it just the sectionClicked() signal which does not work or all signals do not work?
                          • Make sure your headerClicked() has a qDebug() so we are sure it not getting called and/or try a lambda with just qDebug()in it as the slot.

                          If I reset the integrated HeaderView all works.
                          I had integrated debug messages.

                          Signal SectionResized and GeomtryChaged work always. All Mouse Enter/Click/DoubleClick etc. dont work.

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

                          @Andy314
                          On your AdvHeaderView instance what does bool QHeaderView::sectionsClickable() const return? Do you need to call void QHeaderView::setSectionsClickable(bool clickable) with true?

                          Andy314A 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @Andy314
                            On your AdvHeaderView instance what does bool QHeaderView::sectionsClickable() const return? Do you need to call void QHeaderView::setSectionsClickable(bool clickable) with true?

                            Andy314A Offline
                            Andy314A Offline
                            Andy314
                            wrote on last edited by
                            #13

                            @JonB said in QHeaderView induvidual background colors:

                            @Andy314
                            On your AdvHeaderView instance what does bool QHeaderView::sectionsClickable() const return? Do you need to call void QHeaderView::setSectionsClickable(bool clickable) with true?

                            Yeh, that was the problem. Thank you for the tip.

                            JonBJ 1 Reply Last reply
                            0
                            • Andy314A Andy314

                              @JonB said in QHeaderView induvidual background colors:

                              @Andy314
                              On your AdvHeaderView instance what does bool QHeaderView::sectionsClickable() const return? Do you need to call void QHeaderView::setSectionsClickable(bool clickable) with true?

                              Yeh, that was the problem. Thank you for the tip.

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

                              @Andy314
                              What I do not know is why/how a derived QHeaderView does not get this set as a base QHeaderView does. Perhaps a Qt/C++ expert would care to enlighten us?

                              Pl45m4P Andy314A 2 Replies Last reply
                              0
                              • JonBJ JonB

                                @Andy314
                                What I do not know is why/how a derived QHeaderView does not get this set as a base QHeaderView does. Perhaps a Qt/C++ expert would care to enlighten us?

                                Pl45m4P Offline
                                Pl45m4P Offline
                                Pl45m4
                                wrote on last edited by
                                #15

                                @JonB said in QHeaderView induvidual background colors:

                                as a base QHeaderView does.

                                Are you sure about that? Haven't checked the code but this from the documentation

                                Returns true if the header is clickable; otherwise returns false. A clickable header could be set up to allow the user to change the representation of the data in the view related to the header.

                                sounds like it's not the default in QHeaderView


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                JonBJ 1 Reply Last reply
                                0
                                • Pl45m4P Pl45m4

                                  @JonB said in QHeaderView induvidual background colors:

                                  as a base QHeaderView does.

                                  Are you sure about that? Haven't checked the code but this from the documentation

                                  Returns true if the header is clickable; otherwise returns false. A clickable header could be set up to allow the user to change the representation of the data in the view related to the header.

                                  sounds like it's not the default in QHeaderView

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

                                  @Pl45m4

                                  Are you sure about that?

                                  No, I am not sure. I am relying on the OP's statement that with a non-subclassed QHeaderView in his QTableView clicking works (by default) while as soon as he sub-classes to shown AdvHeaderView it does not work. And wondering how that would be.

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @Pl45m4

                                    Are you sure about that?

                                    No, I am not sure. I am relying on the OP's statement that with a non-subclassed QHeaderView in his QTableView clicking works (by default) while as soon as he sub-classes to shown AdvHeaderView it does not work. And wondering how that would be.

                                    Christian EhrlicherC Online
                                    Christian EhrlicherC Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @JonB said in QHeaderView induvidual background colors:

                                    And wondering how that would be.

                                    It was enabled in the ui but since the header gets replaced in c++ it was overridden.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    JonBJ 1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @JonB said in QHeaderView induvidual background colors:

                                      And wondering how that would be.

                                      It was enabled in the ui but since the header gets replaced in c++ it was overridden.

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

                                      @Christian-Ehrlicher said in QHeaderView induvidual background colors:

                                      It was enabled in the ui but since the header gets replaced in c++ it was overridden.

                                      Ah, no wonder, at least that makes sense!

                                      1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @Andy314
                                        What I do not know is why/how a derived QHeaderView does not get this set as a base QHeaderView does. Perhaps a Qt/C++ expert would care to enlighten us?

                                        Andy314A Offline
                                        Andy314A Offline
                                        Andy314
                                        wrote on last edited by Andy314
                                        #19

                                        @JonB said in QHeaderView induvidual background colors:

                                        @Andy314
                                        What I do not know is why/how a derived QHeaderView does not get this set as a base QHeaderView does. Perhaps a Qt/C++ expert would care to enlighten us?

                                        That is exact the problem I struggled.
                                        From the logic it can only be that the QTableView itself configures its QHeaderView correct.
                                        Indeed I found the code for it:

                                        void QTableViewPrivate::init()
                                        {
                                            Q_Q(QTableView);
                                            q->setEditTriggers(editTriggers|QAbstractItemView::AnyKeyPressed);
                                            QHeaderView *vertical = new QHeaderView(Qt::Vertical, q);
                                            vertical->setSectionsClickable(true);
                                            vertical->setHighlightSections(true);
                                            q->setVerticalHeader(vertical);
                                            QHeaderView *horizontal = new QHeaderView(Qt::Horizontal, q);
                                            horizontal->setSectionsClickable(true);  // Here is the code ****************************
                                            horizontal->setHighlightSections(true);
                                            q->setHorizontalHeader(horizontal);
                                            tabKeyNavigation = true;
                                        #if QT_CONFIG(abstractbutton)
                                            cornerWidget = new QTableCornerButton(q);
                                            cornerWidget->setFocusPolicy(Qt::NoFocus);
                                            QObject::connect(cornerWidget, SIGNAL(clicked()), 
                                        q, SLOT(selectAll()));
                                        #endif
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • Andy314A Andy314 has marked this topic as solved on

                                        • Login

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