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 9.4k 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.
  • C Chris Kawa
    18 Jun 2021, 19:49

    @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 18 Jun 2021, 20:31 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 22 Jul 2022, 07:34 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.

      J 1 Reply Last reply 22 Jul 2022, 07:44
      0
      • N nonskill
        22 Jul 2022, 07:34

        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.

        J Offline
        J Offline
        JonB
        wrote on 22 Jul 2022, 07:44 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