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. changing the checkstate of all the child item by checking the root item in Qtree widget
Forum Updated to NodeBB v4.3 + New Features

changing the checkstate of all the child item by checking the root item in Qtree widget

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 2.9k Views
  • 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.
  • K kook

    @jsulm yes i read that document and tried using it by creating a simple code

    QTreeWidgetItemIterator iterator(item);
        while (*iterator)
        {
            if ((*iterator)->checkState(0) == Qt::Checked)
            {
                qDebug()<< (*iterator)->text(0);
            }
            ++iterator;
        }
    

    but i am unable to achieve want i wanted (checking all the child item by checking the root/parent) using this
    what i am asking maybe a very simple thing but i am finding it difficult since i am a beginner please help me out thank you

    B Offline
    B Offline
    Bob64
    wrote on last edited by
    #6

    @kook you aren't changing anything in that loop. Don't you need to call setCheckState? See the documentation you have been pointed to; there is a very similar example.

    1 Reply Last reply
    1
    • K Offline
      K Offline
      kook
      wrote on last edited by
      #7
      void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
      {
          QTreeWidgetItemIterator iterator(item);
          while( *iterator)
          {
              if ( (*iterator)->childCount() != 0)
              {
                 if ( (*iterator)->checkState(column) == Qt::Checked)
                 {
                     for (int i = 0; i < item->childCount(); ++i)
                     {
                         (*iterator)->child(i)->setCheckState(0, Qt::Checked);
                     }
                 }
              }
              ++iterator;
          }
      

      ok i am able to do it now tell me is this the correct way of doing

      jsulmJ 1 Reply Last reply
      0
      • K kook
        void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
        {
            QTreeWidgetItemIterator iterator(item);
            while( *iterator)
            {
                if ( (*iterator)->childCount() != 0)
                {
                   if ( (*iterator)->checkState(column) == Qt::Checked)
                   {
                       for (int i = 0; i < item->childCount(); ++i)
                       {
                           (*iterator)->child(i)->setCheckState(0, Qt::Checked);
                       }
                   }
                }
                ++iterator;
            }
        

        ok i am able to do it now tell me is this the correct way of doing

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #8

        @kook Why iterator->child?!
        First, check the check state of the parent, then apply that state on the children.

        auto state = item->checkState(0);
        QTreeWidgetItemIterator iterator(item);
        while (*iterator)
        {
                qDebug()<< (*iterator)->text(0);
                (*iterator)->setCheckState(0, state);
                ++iterator;
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        K JonBJ 3 Replies Last reply
        2
        • jsulmJ jsulm

          @kook Why iterator->child?!
          First, check the check state of the parent, then apply that state on the children.

          auto state = item->checkState(0);
          QTreeWidgetItemIterator iterator(item);
          while (*iterator)
          {
                  qDebug()<< (*iterator)->text(0);
                  (*iterator)->setCheckState(0, state);
                  ++iterator;
          }
          
          K Offline
          K Offline
          kook
          wrote on last edited by
          #9

          @jsulm thank you i will try this

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @kook Why iterator->child?!
            First, check the check state of the parent, then apply that state on the children.

            auto state = item->checkState(0);
            QTreeWidgetItemIterator iterator(item);
            while (*iterator)
            {
                    qDebug()<< (*iterator)->text(0);
                    (*iterator)->setCheckState(0, state);
                    ++iterator;
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #10

            @jsulm
            Does the (*iterator)->setCheckState(0, state); re-fire the on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) for the child item's check state being changed? If so, good; if not, the OP will need a recursive function assuming he wants all descendants to get checked (and tree depth not limited to one level).

            jsulmJ 1 Reply Last reply
            0
            • JonBJ JonB

              @jsulm
              Does the (*iterator)->setCheckState(0, state); re-fire the on_treeWidget_itemChanged(QTreeWidgetItem *item, int column) for the child item's check state being changed? If so, good; if not, the OP will need a recursive function assuming he wants all descendants to get checked (and tree depth not limited to one level).

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @JonB said in changing the checkstate of all the child item by checking the root item in Qtree widget:

              if not, the OP will need a recursive function assuming he wants all descendants to get checked

              No, that's what QTreeWidgetItemIterator is doing.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @kook Why iterator->child?!
                First, check the check state of the parent, then apply that state on the children.

                auto state = item->checkState(0);
                QTreeWidgetItemIterator iterator(item);
                while (*iterator)
                {
                        qDebug()<< (*iterator)->text(0);
                        (*iterator)->setCheckState(0, state);
                        ++iterator;
                }
                
                K Offline
                K Offline
                kook
                wrote on last edited by
                #12

                @jsulm the reason i used (*iterator)->child is i have multiple root in the tree i want only that particular root's child to be checked not the whole tree
                the solution you have given is doing the latter

                jsulmJ 1 Reply Last reply
                0
                • K kook

                  @jsulm the reason i used (*iterator)->child is i have multiple root in the tree i want only that particular root's child to be checked not the whole tree
                  the solution you have given is doing the latter

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @kook The solution I gave you iterates over all children of an item, NOT the whole tree.
                  If you want to change only some children of an item then you need a condition to decide which one to change, right? What is this condition? Can you explain what exactly should happen?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @kook The solution I gave you iterates over all children of an item, NOT the whole tree.
                    If you want to change only some children of an item then you need a condition to decide which one to change, right? What is this condition? Can you explain what exactly should happen?

                    K Offline
                    K Offline
                    kook
                    wrote on last edited by
                    #14

                    @jsulm Screenshot from 2021-10-11 10-56-53.png
                    when i change the message1 state it should change only its children check state
                    i tried the code you provided its changing the whole tree check state

                    i don't have any specific condition

                    jsulmJ 1 Reply Last reply
                    0
                    • K kook

                      @jsulm Screenshot from 2021-10-11 10-56-53.png
                      when i change the message1 state it should change only its children check state
                      i tried the code you provided its changing the whole tree check state

                      i don't have any specific condition

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:

                      i tried the code you provided its changing the whole tree check state

                      Then you did something wrong.
                      What do you pass as item in this line QTreeWidgetItemIterator iterator(item); ?
                      It needs to be the item user clicked (in your example message1).

                      auto state = item->checkState(0);
                      QTreeWidgetItemIterator iterator(item);
                      while (*iterator)
                      {
                              qDebug()<< (*iterator)->text(0);
                              (*iterator)->setCheckState(0, state);
                              ++iterator;
                      }
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      K 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:

                        i tried the code you provided its changing the whole tree check state

                        Then you did something wrong.
                        What do you pass as item in this line QTreeWidgetItemIterator iterator(item); ?
                        It needs to be the item user clicked (in your example message1).

                        auto state = item->checkState(0);
                        QTreeWidgetItemIterator iterator(item);
                        while (*iterator)
                        {
                                qDebug()<< (*iterator)->text(0);
                                (*iterator)->setCheckState(0, state);
                                ++iterator;
                        }
                        
                        K Offline
                        K Offline
                        kook
                        wrote on last edited by
                        #16

                        @jsulm i am adding the code inside this function

                        void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
                        {
                        }
                        jsulmJ 2 Replies Last reply
                        0
                        • K kook

                          @jsulm i am adding the code inside this function

                          void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
                          {
                          }
                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #17

                          @kook You're right. This does not work as expected.
                          You will need to write a recursive method to iterate over all children of an item.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          K 1 Reply Last reply
                          0
                          • K kook

                            @jsulm i am adding the code inside this function

                            void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
                            {
                            }
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #18

                            @kook This works:

                            void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
                                for (int i = 0; i < item->childCount(); ++i) {
                                    qDebug() << item->child(i)->text(0);
                                    on_treeWidget_itemChanged(item->child(i), column);
                                }
                            }
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @kook You're right. This does not work as expected.
                              You will need to write a recursive method to iterate over all children of an item.

                              K Offline
                              K Offline
                              kook
                              wrote on last edited by
                              #19

                              @jsulm ok i will try thank you

                              jsulmJ 1 Reply Last reply
                              0
                              • K kook

                                @jsulm ok i will try thank you

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                @kook I just fixed my code...

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                K 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @kook I just fixed my code...

                                  K Offline
                                  K Offline
                                  kook
                                  wrote on last edited by
                                  #21

                                  @jsulm its not changing the checked state of children

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • K kook

                                    @jsulm its not changing the checked state of children

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by jsulm
                                    #22

                                    @kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:

                                    its not changing the checked state of children

                                    Come on, that's because I did not add the code to change the checked state. Add that part:

                                    void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
                                        for (int i = 0; i < item->childCount(); ++i) {
                                            qDebug() << item->child(i)->text(0);
                                            item->child(i)->setCheckState(0, item->checkState(0));
                                            on_treeWidget_itemChanged(item->child(i), column);
                                        }
                                    }
                                    

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    K 1 Reply Last reply
                                    3
                                    • jsulmJ jsulm

                                      @kook said in changing the checkstate of all the child item by checking the root item in Qtree widget:

                                      its not changing the checked state of children

                                      Come on, that's because I did not add the code to change the checked state. Add that part:

                                      void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
                                          for (int i = 0; i < item->childCount(); ++i) {
                                              qDebug() << item->child(i)->text(0);
                                              item->child(i)->setCheckState(0, item->checkState(0));
                                              on_treeWidget_itemChanged(item->child(i), column);
                                          }
                                      }
                                      
                                      K Offline
                                      K Offline
                                      kook
                                      wrote on last edited by
                                      #23

                                      @jsulm sorry i added that part its working
                                      thank you for your time and solution

                                      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