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. Add a QCheckBox as a header to a QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Add a QCheckBox as a header to a QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 6 Posters 10.2k Views 3 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.
  • H hbatalha

    @mrjj I honestly was expecting something a lot simpler.

    @Chris-Kawa the example the mousepress event doesn't work very well when I use it.
    I have a dialog which its class is a member of MainWindow, I have an action that when triggered will open that dialog that contains the tablewidget, when I have it open and I click on the box I have to close the dialog and open it again to see that it's been checked and vice-versa.

    Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #5

    @hbatalha said in Add a QCheckBox as a header to a QTableWidget:

    when I have it open and I click on the box I have to close the dialog and open it again to see that it's been checked and vice-versa.

    Then you must've done something differently. The example I linked to certainly does not behave like that and I see nothing in it that would make it so. Can you share your custom header implementation and the part of code where you create the dialog and set the header?

    H 1 Reply Last reply
    1
    • Chris KawaC Chris Kawa

      @hbatalha said in Add a QCheckBox as a header to a QTableWidget:

      when I have it open and I click on the box I have to close the dialog and open it again to see that it's been checked and vice-versa.

      Then you must've done something differently. The example I linked to certainly does not behave like that and I see nothing in it that would make it so. Can you share your custom header implementation and the part of code where you create the dialog and set the header?

      H Offline
      H Offline
      hbatalha
      wrote on last edited by
      #6

      @Chris-Kawa

      Then you must've done something differently.

      I just copied and pasted.

      Can you share your custom header implementation and the part of code where you create the dialog and set the header?

      class MyHeader : public QHeaderView
      {
      public:
      
          MyHeader(Qt::Orientation orientation, QWidget * parent = nullptr) : QHeaderView(orientation, parent)
          {}
      protected:
      
          void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
          {
              painter->save();
              QHeaderView::paintSection(painter, rect, logicalIndex);
              painter->restore();
              if (logicalIndex == 0)
              {
                  QStyleOptionButton option;
                  option.rect = QRect(5,5,15,15);
                  if (isOn)
                      option.state = QStyle::State_On;
                  else
                      option.state = QStyle::State_Off;
                  this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
              }
          }
      
          void mousePressEvent(QMouseEvent *event)
          {
              qDebug() << "here";
              if (isOn)
                  isOn = false;
              else
                  isOn = true;
              this->update();
              QHeaderView::mousePressEvent(event);
          }
      private:
      
          bool isOn;
      };
      
      
      
      HistoryWindow::HistoryWindow(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::historyWindow)
      {
          ui->setupUi(this);
      
          this->setWindowTitle(tr("History"));
      
          ui->search_lineEdit->setPlaceholderText(tr("Search"));
      
          setStyleSheet("QLineEdit{"
                        "    color: white;" //TEXT COLOR
                        "}"
                        "QLineEdit[text=\"\"]{"
                        "    color: gray;" //TEXTHOLDER COLOR
                        "}");
          connect(ui->search_lineEdit, &QLineEdit::textChanged, [=] { style()->polish(ui->search_lineEdit); });
      
          ui->search_lineEdit->setFocusPolicy(Qt::ClickFocus);
      
          MyHeader* h = new MyHeader(Qt::Horizontal, ui->table);
          ui->table->setHorizontalHeader(h);
      
          ui->table->setColumnWidth(0, 318);
          ui->table->setColumnWidth(1, 70);
          ui->table->setShowGrid(false);
          ui->table->horizontalScrollBar()->hide();
          ui->table->setSelectionMode(QAbstractItemView::NoSelection);
          ui->table->setFocusPolicy(Qt::NoFocus);
          ui->search_lineEdit->clearFocus();
      
      }
      
      HistoryWindow::~HistoryWindow()
      {
          delete ui;
      }
      
      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #7

        Sigh... right, don't you just love when internet examples are buggy? :P

        Here's a fixed version:

        class MyHeader : public QHeaderView
        {
        public:
            using QHeaderView::QHeaderView;
        
        protected:
        
            void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override
            {
                painter->save();
                QHeaderView::paintSection(painter, rect, logicalIndex);
                painter->restore();
        
                if (model() && logicalIndex >= 0)
                {
                    QStyleOptionButton option;
                    option.init(this);
        
                    QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this);
                    checkbox_rect.moveCenter(rect.center());
        
                    bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool();
        
                    option.rect = checkbox_rect;
                    option.state = checked ? QStyle::State_On : QStyle::State_Off;
        
                    style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
                }
            }
        
            void mouseReleaseEvent(QMouseEvent* event) override
            {
                QHeaderView::mouseReleaseEvent(event);
                if(model())
                {
                    int section = logicalIndexAt(event->pos());
                    if (section >= 0)
                    {
                        bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool();
                        model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole);
                        viewport()->update();
                    }
                }
            }
        };
        

        Just a note - for simplicity this version toggles the checkbox whenever you click anywhere in the header section. You should probably shrink this area to only the actual checkbox. You have all the info in the example - just get the checkbox rectangle and see if the click was inside it.

        H 3 Replies Last reply
        2
        • Chris KawaC Chris Kawa

          Sigh... right, don't you just love when internet examples are buggy? :P

          Here's a fixed version:

          class MyHeader : public QHeaderView
          {
          public:
              using QHeaderView::QHeaderView;
          
          protected:
          
              void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override
              {
                  painter->save();
                  QHeaderView::paintSection(painter, rect, logicalIndex);
                  painter->restore();
          
                  if (model() && logicalIndex >= 0)
                  {
                      QStyleOptionButton option;
                      option.init(this);
          
                      QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this);
                      checkbox_rect.moveCenter(rect.center());
          
                      bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool();
          
                      option.rect = checkbox_rect;
                      option.state = checked ? QStyle::State_On : QStyle::State_Off;
          
                      style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
                  }
              }
          
              void mouseReleaseEvent(QMouseEvent* event) override
              {
                  QHeaderView::mouseReleaseEvent(event);
                  if(model())
                  {
                      int section = logicalIndexAt(event->pos());
                      if (section >= 0)
                      {
                          bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool();
                          model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole);
                          viewport()->update();
                      }
                  }
              }
          };
          

          Just a note - for simplicity this version toggles the checkbox whenever you click anywhere in the header section. You should probably shrink this area to only the actual checkbox. You have all the info in the example - just get the checkbox rectangle and see if the click was inside it.

          H Offline
          H Offline
          hbatalha
          wrote on last edited by hbatalha
          #8

          @Chris-Kawa thanks for the code.

          I believe that instead of this line

          option.init(this);

          you meant
          option.initFrom(this); becasue the compiler complains about the non-existence of init

          A question:
          How can I add MyHeader only to the first colum like in the buggy example, instead of adding to all the columns in the header?

          Edit: To add MyHeader only to the first colum I just edited this line:

          if (model() && logicalIndex >= 0)
          

          to

          if (model() && logicalIndex == 0)
          
          1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            Sigh... right, don't you just love when internet examples are buggy? :P

            Here's a fixed version:

            class MyHeader : public QHeaderView
            {
            public:
                using QHeaderView::QHeaderView;
            
            protected:
            
                void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override
                {
                    painter->save();
                    QHeaderView::paintSection(painter, rect, logicalIndex);
                    painter->restore();
            
                    if (model() && logicalIndex >= 0)
                    {
                        QStyleOptionButton option;
                        option.init(this);
            
                        QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this);
                        checkbox_rect.moveCenter(rect.center());
            
                        bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool();
            
                        option.rect = checkbox_rect;
                        option.state = checked ? QStyle::State_On : QStyle::State_Off;
            
                        style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
                    }
                }
            
                void mouseReleaseEvent(QMouseEvent* event) override
                {
                    QHeaderView::mouseReleaseEvent(event);
                    if(model())
                    {
                        int section = logicalIndexAt(event->pos());
                        if (section >= 0)
                        {
                            bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool();
                            model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole);
                            viewport()->update();
                        }
                    }
                }
            };
            

            Just a note - for simplicity this version toggles the checkbox whenever you click anywhere in the header section. You should probably shrink this area to only the actual checkbox. You have all the info in the example - just get the checkbox rectangle and see if the click was inside it.

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #9

            @Chris-Kawa How would I implement a clicked() signal mechanism?

            mrjjM 1 Reply Last reply
            0
            • H hbatalha

              @Chris-Kawa How would I implement a clicked() signal mechanism?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #10

              @hbatalha

              Hi
              you would define a signal in .h ( in the class)

              ..
              signals:
              void Clicked();

              then use it with the keyword emit
              in void mouseReleaseEvent(QMouseEvent* event)
              in the spot where you check/uncheck ( maybe after viewport()->update(); )
              like

              emit clicked();
              

              Do not i didn't put a parameter here to the clicked signal but in real world you might want to consider adding an int/id to say which section was clicked.

              1 Reply Last reply
              4
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #11

                Just to add to what @mrjj said - a click is when you press and release mouse button on the same widget.

                Having said that you shouldn't just always emit it in release event. You should record in press event which section was pressed, then in release check if it's the same one and only then emit a clicked signal.

                If you release on different section than the one you press on nothing should happen. This is for the purpose of user realizing mid-click that they pressed on the wrong area. They can then move the mouse away and release and this is basically cancelling the click.

                H 1 Reply Last reply
                3
                • Chris KawaC Chris Kawa

                  Just to add to what @mrjj said - a click is when you press and release mouse button on the same widget.

                  Having said that you shouldn't just always emit it in release event. You should record in press event which section was pressed, then in release check if it's the same one and only then emit a clicked signal.

                  If you release on different section than the one you press on nothing should happen. This is for the purpose of user realizing mid-click that they pressed on the wrong area. They can then move the mouse away and release and this is basically cancelling the click.

                  H Offline
                  H Offline
                  hbatalha
                  wrote on last edited by hbatalha
                  #12

                  @Chris-Kawa @mrjj Done.

                  Is there a way to control the checkbox programatically, sending the signal and get the checkbox checked. I need the checkbox default state to be checked and I need to uncheck it when I uncheck any checkbox in the rows in the qtablewidget. Like in the OP image.

                  mrjjM 1 Reply Last reply
                  1
                  • H hbatalha

                    @Chris-Kawa @mrjj Done.

                    Is there a way to control the checkbox programatically, sending the signal and get the checkbox checked. I need the checkbox default state to be checked and I need to uncheck it when I uncheck any checkbox in the rows in the qtablewidget. Like in the OP image.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @hbatalha
                    hi
                    it uses the model so you can just set it in model

                     model->setHeaderData(section, orientation(), true/false, Qt::CheckStateRole);
                    
                    H 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @hbatalha
                      hi
                      it uses the model so you can just set it in model

                       model->setHeaderData(section, orientation(), true/false, Qt::CheckStateRole);
                      
                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #14

                      @mrjj said in Add a QCheckBox as a header to a QTableWidget:

                      model->setHeaderData(section, orientation(), true/false, Qt::CheckStateRole);

                      what would be the value of section?

                      mrjjM 1 Reply Last reply
                      0
                      • H hbatalha

                        @mrjj said in Add a QCheckBox as a header to a QTableWidget:

                        model->setHeaderData(section, orientation(), true/false, Qt::CheckStateRole);

                        what would be the value of section?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #15

                        @hbatalha
                        just 0,1,2,3,4 for the sections.
                        unless you allow drag to reorder then you have to use
                        https://doc.qt.io/qt-5/qheaderview.html#visualIndex
                        to map to the right index/ID

                        H L 2 Replies Last reply
                        2
                        • mrjjM mrjj

                          @hbatalha
                          just 0,1,2,3,4 for the sections.
                          unless you allow drag to reorder then you have to use
                          https://doc.qt.io/qt-5/qheaderview.html#visualIndex
                          to map to the right index/ID

                          H Offline
                          H Offline
                          hbatalha
                          wrote on last edited by
                          #16

                          @mrjj I gave it 0.

                          Everything is working perfect.

                          Thank you all!!!

                          1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @hbatalha
                            just 0,1,2,3,4 for the sections.
                            unless you allow drag to reorder then you have to use
                            https://doc.qt.io/qt-5/qheaderview.html#visualIndex
                            to map to the right index/ID

                            L Offline
                            L Offline
                            lisukovigor492
                            wrote on last edited by lisukovigor492
                            #17

                            @mrjj just 0,1,2,3,4 for the sections.
                            unless you allow drag to reorder then you have to use
                            https://doc.qt.io/qt-5/qheaderview.html#visualIndex https://www.worktime.com/employee-time-tracking-software
                            to map to the right index/ID

                            Hello,
                            the question is too simple, but I'm a beginner, please tell me what parameters of the indexer should be?

                            Thank you

                            mrjjM 1 Reply Last reply
                            0
                            • L lisukovigor492

                              @mrjj just 0,1,2,3,4 for the sections.
                              unless you allow drag to reorder then you have to use
                              https://doc.qt.io/qt-5/qheaderview.html#visualIndex https://www.worktime.com/employee-time-tracking-software
                              to map to the right index/ID

                              Hello,
                              the question is too simple, but I'm a beginner, please tell me what parameters of the indexer should be?

                              Thank you

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #18

                              @lisukovigor492
                              Hi
                              you mean for the
                              int QHeaderView::visualIndex(int logicalIndex) ?

                              well its for when its reordered. then say index 5 is in front and then comes 2 and so on.
                              The issues is they are no longer in order.

                              so you use

                              int realIndex = HeaderView->visualIndex(0)
                              and IF the header is reorderd it will then tell you what index is a section 0.
                              like 7. or what ever what dragged there.

                              there is also
                              int QHeaderView::logicalIndex(int visualIndex)

                              I might recall it reverse. :) but they allow to map between the sections you see on screen and their real Id/indexs.

                              But this is only important if you allow user to move the sections around.

                              To get to know them, you could enable it and then
                              drag some around and the see what therse functions returns for the sections.

                              its all just integers values so nothing fancy at all.

                              1 Reply Last reply
                              0
                              • Chris KawaC Chris Kawa

                                Sigh... right, don't you just love when internet examples are buggy? :P

                                Here's a fixed version:

                                class MyHeader : public QHeaderView
                                {
                                public:
                                    using QHeaderView::QHeaderView;
                                
                                protected:
                                
                                    void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override
                                    {
                                        painter->save();
                                        QHeaderView::paintSection(painter, rect, logicalIndex);
                                        painter->restore();
                                
                                        if (model() && logicalIndex >= 0)
                                        {
                                            QStyleOptionButton option;
                                            option.init(this);
                                
                                            QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this);
                                            checkbox_rect.moveCenter(rect.center());
                                
                                            bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool();
                                
                                            option.rect = checkbox_rect;
                                            option.state = checked ? QStyle::State_On : QStyle::State_Off;
                                
                                            style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
                                        }
                                    }
                                
                                    void mouseReleaseEvent(QMouseEvent* event) override
                                    {
                                        QHeaderView::mouseReleaseEvent(event);
                                        if(model())
                                        {
                                            int section = logicalIndexAt(event->pos());
                                            if (section >= 0)
                                            {
                                                bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool();
                                                model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole);
                                                viewport()->update();
                                            }
                                        }
                                    }
                                };
                                

                                Just a note - for simplicity this version toggles the checkbox whenever you click anywhere in the header section. You should probably shrink this area to only the actual checkbox. You have all the info in the example - just get the checkbox rectangle and see if the click was inside it.

                                H Offline
                                H Offline
                                hbatalha
                                wrote on last edited by
                                #19

                                @Chris-Kawa is there a way to change the checkbox border and mark color so it doesn't look grayed outScreenshot_1.png

                                I have tried

                                QPalette p(Qt::black);
                                style()->polish(p);
                                
                                painter->setPen(Qt::black);
                                painter->setBrush(Qt::back);
                                
                                option.palette = QPalette(Qt::black);
                                

                                But none of the above worked.
                                Is there a way to change its color?

                                Chris KawaC 1 Reply Last reply
                                0
                                • H hbatalha

                                  @Chris-Kawa is there a way to change the checkbox border and mark color so it doesn't look grayed outScreenshot_1.png

                                  I have tried

                                  QPalette p(Qt::black);
                                  style()->polish(p);
                                  
                                  painter->setPen(Qt::black);
                                  painter->setBrush(Qt::back);
                                  
                                  option.palette = QPalette(Qt::black);
                                  

                                  But none of the above worked.
                                  Is there a way to change its color?

                                  Chris KawaC Offline
                                  Chris KawaC Offline
                                  Chris Kawa
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #20

                                  @hbatalha It's an image so changing pen, brush or palette doesn't affect it. It might appear disabled if you give it the wrong state flags when painting, so check that the state member is set correctly in the style option.
                                  The checkbox also appears crooked in your screenshot, which suggests the rect member is set incorrectly, so again make sure you set all the members of your style options object correctly before painting with it.

                                  H 1 Reply Last reply
                                  1
                                  • Chris KawaC Chris Kawa

                                    @hbatalha It's an image so changing pen, brush or palette doesn't affect it. It might appear disabled if you give it the wrong state flags when painting, so check that the state member is set correctly in the style option.
                                    The checkbox also appears crooked in your screenshot, which suggests the rect member is set incorrectly, so again make sure you set all the members of your style options object correctly before painting with it.

                                    H Offline
                                    H Offline
                                    hbatalha
                                    wrote on last edited by hbatalha
                                    #21

                                    @Chris-Kawa said in Add a QCheckBox as a header to a QTableWidget:

                                    so again make sure you set all the members of your style options object correctly before painting with it.

                                    The code is like the one you provided in your answer above:

                                    class CheckBox : public QHeaderView
                                    {
                                        Q_OBJECT
                                    
                                    public:
                                        using QHeaderView::QHeaderView;
                                    
                                    signals:
                                        void clicked(bool);
                                    
                                    protected:
                                    
                                        void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override
                                        {
                                            painter->save();
                                            QHeaderView::paintSection(painter, rect, logicalIndex);
                                            painter->restore();
                                    
                                            if (model() && logicalIndex == 0)
                                            {
                                                QStyleOptionButton option;
                                                option.initFrom(this);
                                    
                                                QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this);
                                                checkbox_rect.moveLeft(rect.left());
                                    
                                                bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool();
                                    
                                                option.rect = checkbox_rect;
                                                option.state = checked ? QStyle::State_On : QStyle::State_Off;
                                    
                                                style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
                                            }
                                        }
                                    
                                    public:
                                    
                                        void mouseReleaseEvent(QMouseEvent* event) override
                                        {
                                            QHeaderView::mouseReleaseEvent(event);
                                            if(model())
                                            {
                                                int section = logicalIndexAt(event->pos());
                                                if (section >= 0)
                                                {
                                                    bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool();
                                                    model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole);
                                                    viewport()->update();
                                                    emit clicked( ! checked);
                                                }
                                            }
                                        }
                                    };
                                    

                                    Edit: The problem is only on windows for the looks of it as I just tested it on linux mint 20.1 and it looks totally fine

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      nonskill
                                      wrote on last edited by
                                      #22

                                      No one mentioned how to add Checkbox in QTableWidget header with PartiallyChecked support, anyone knowns how to make it? There only option for Checkbox is just QStyle::State_On and QStyle::State_Off, which is far from functional. The most important option QStyle::State_Partial is not found.

                                      JonBJ 1 Reply Last reply
                                      0
                                      • N nonskill

                                        No one mentioned how to add Checkbox in QTableWidget header with PartiallyChecked support, anyone knowns how to make it? There only option for Checkbox is just QStyle::State_On and QStyle::State_Off, which is far from functional. The most important option QStyle::State_Partial is not found.

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

                                        @nonskill
                                        Did you try QStyle::State_NoChange ("Used to indicate a tri-state checkbox.") from flags QStyle::State?

                                        1 Reply Last reply
                                        1

                                        • Login

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