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. Want to have checkboxes in headers of QTreeWiddget
Forum Updated to NodeBB v4.3 + New Features

Want to have checkboxes in headers of QTreeWiddget

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 3.8k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by Qt Enthusiast
    #1

    I have QTreeWidget and I want to create checkboxes in QtreeWidget headers
    for example

    I tried following code and it is not working

    QTreeWidget myTree = new QtreeWidget;
    myTree->setHeaderLabels(QStringList() << tr("name") << tr ("v") << tr("v"));

    myTree->setColumnCount(3);
    myTree->setHeaderLabels(QStringList() << tr("name") << tr("v") << tr("s"));
    QTreeWidgetItem * header =myTree->headerItem();
    header->setCheckState(1,Qt::Checked);
    header->setCheckState(2,Qt::Checked);

    and it is not working Can someone guide me on the same

    mrjjM 1 Reply Last reply
    0
    • Q Qt Enthusiast

      I have QTreeWidget and I want to create checkboxes in QtreeWidget headers
      for example

      I tried following code and it is not working

      QTreeWidget myTree = new QtreeWidget;
      myTree->setHeaderLabels(QStringList() << tr("name") << tr ("v") << tr("v"));

      myTree->setColumnCount(3);
      myTree->setHeaderLabels(QStringList() << tr("name") << tr("v") << tr("s"));
      QTreeWidgetItem * header =myTree->headerItem();
      header->setCheckState(1,Qt::Checked);
      header->setCheckState(2,Qt::Checked);

      and it is not working Can someone guide me on the same

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

      @Qt-Enthusiast
      hi
      THis should give good hints
      http://blog.qt.io/blog/2014/04/11/qt-weekly-5-widgets-on-a-qheaderview/

      mrjjM 1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F

        P.S.
        This was basically a LMGTFY... 😒

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        Q 1 Reply Last reply
        3
        • VRoninV VRonin

          https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F

          P.S.
          This was basically a LMGTFY... 😒

          Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by
          #4

          @VRonin

          The check box in this not clickable

          mrjjM Q 2 Replies Last reply
          0
          • Q Qt Enthusiast

            @VRonin

            The check box in this not clickable

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

            @Qt-Enthusiast
            Hi
            That part is left as an exercise for the reader.

            "You would also need to reimplement the mousePressEvent() http://doc.qt.io/qt-5/qheaderview.html#mousePressEvent to detect when the checkbox is clicked, in order to paint the checked and unchecked states."

            Q 1 Reply Last reply
            2
            • mrjjM mrjj

              @Qt-Enthusiast
              Hi
              That part is left as an exercise for the reader.

              "You would also need to reimplement the mousePressEvent() http://doc.qt.io/qt-5/qheaderview.html#mousePressEvent to detect when the checkbox is clicked, in order to paint the checked and unchecked states."

              Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #6

              @mrjj
              it is there

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                Is it possible to have these headers per column basis, I don not see anything in such documenattion

                1 Reply Last reply
                0
                • mrjjM mrjj

                  @Qt-Enthusiast
                  hi
                  THis should give good hints
                  http://blog.qt.io/blog/2014/04/11/qt-weekly-5-widgets-on-a-qheaderview/

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

                  @mrjj
                  http://blog.qt.io/blog/2014/04/11/qt-weekly-5-widgets-on-a-qheaderview/
                  Actually this sample have issues
                  It wont call paintSection every time and hence its not redrawn correctly.

                  It seems that
                  this->update();
                  will not always trigger it to paint whole header. ( call paintSection)

                  Anyone have good input on why ?

                  
                  #include <QtGui>
                  #include <QHeaderView>
                  #include <QTableWidget>
                  #include <QDebug>
                  
                  class MyHeader : public QHeaderView {
                   public:
                    MyHeader(Qt::Orientation orientation, QWidget* parent = 0) : QHeaderView(orientation, parent) {
                    }
                  
                   protected:
                    void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const {
                      painter->save();
                      QHeaderView::paintSection(painter, rect, logicalIndex);
                      painter->restore();
                      qDebug() << "Paint FIRST";
                      if (logicalIndex == 0) {
                        QStyleOptionButton option;
                        option.rect = QRect(10, 10, 10, 10);
                        if (isOn)
                          option.state = QStyle::State_On;
                        else
                          option.state = QStyle::State_Off;
                        this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
                        qDebug() << "Paint logicalIndex";
                      }
                  
                    }
                    void mousePressEvent(QMouseEvent* event) {
                      QHeaderView::mousePressEvent(event);
                  
                      if (isOn)
                        isOn = false;
                      else
                        isOn = true;
                  
                      this->update();
                  
                      qDebug() << "mousePressEvent";
                    }
                  
                   private:
                    bool isOn;  
                  protected:
                  };
                  
                  
                  int main(int argc, char** argv) {
                    QApplication app(argc, argv);
                    QTableWidget table;
                    table.setRowCount(4);
                    table.setColumnCount(3);
                  
                    MyHeader* myHeader = new MyHeader(Qt::Horizontal, &table);
                    table.setHorizontalHeader(myHeader);
                    table.show();
                    return app.exec();
                  }
                  
                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #9

                    Please suggest the solution

                    1 Reply Last reply
                    0
                    • Q Qt Enthusiast

                      @VRonin

                      The check box in this not clickable

                      Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by
                      #10

                      @Qt-Enthusiast the

                      the check box is not easily clickable if you can suggest the proper solution

                      mrjjM 1 Reply Last reply
                      0
                      • Q Qt Enthusiast

                        @Qt-Enthusiast the

                        the check box is not easily clickable if you can suggest the proper solution

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

                        @Qt-Enthusiast
                        Did not find any.
                        The sample listed on wiki do not repaint headers (always) so that did not work well.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Qt Enthusiast
                          wrote on last edited by
                          #12

                          Can u suggest some alternative solution

                          mrjjM 1 Reply Last reply
                          0
                          • Q Qt Enthusiast

                            Can u suggest some alternative solution

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

                            @Qt-Enthusiast
                            Hi Sadly no. The sample would work if update() would make the header to be updated but
                            it do not as far as i can tell.

                            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