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 Update on Monday, May 27th 2025

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.7k 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 Offline
    K Offline
    kook
    wrote on last edited by
    #1

    Hello
    I want to change the checkstate of the tree items like fallows

    1. if i click on the child item it should change (this i am able to do)
    2. if i click on the root it should change the check state of child items too

    here is the code

            ```
            item->setExpanded(true);
            item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
            item->setCheckState(0, Qt::Unchecked);
    
    void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
    {
             if ( item->checkState(column) == Qt::Checked)
        {
                  qDebug() << item->text(column);
        }
    }
    

    and after checking i want to store all the checked items to a list please someone help me out in this

    Thanks in advance

    jsulmJ 1 Reply Last reply
    0
    • K kook

      Hello
      I want to change the checkstate of the tree items like fallows

      1. if i click on the child item it should change (this i am able to do)
      2. if i click on the root it should change the check state of child items too

      here is the code

              ```
              item->setExpanded(true);
              item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
              item->setCheckState(0, Qt::Unchecked);
      
      void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
      {
               if ( item->checkState(column) == Qt::Checked)
          {
                    qDebug() << item->text(column);
          }
      }
      

      and after checking i want to store all the checked items to a list please someone help me out in this

      Thanks in advance

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

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

      and after checking i want to store all the checked items to a list

      You asked this already here: https://forum.qt.io/topic/130848/read-and-store-the-checked-items-from-the-qtreewidget/3
      What exactly is the problem with this?

      Regarding your other question: you can use https://doc.qt.io/qt-5/qtreewidgetitemiterator.html to iterate over all children of an item.

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

      K 1 Reply Last reply
      2
      • jsulmJ jsulm

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

        and after checking i want to store all the checked items to a list

        You asked this already here: https://forum.qt.io/topic/130848/read-and-store-the-checked-items-from-the-qtreewidget/3
        What exactly is the problem with this?

        Regarding your other question: you can use https://doc.qt.io/qt-5/qtreewidgetitemiterator.html to iterate over all children of an item.

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

        @jsulm my promblem is i am able to check the child item
        but i want to check alll the child items just by checking the root item

        jsulmJ 1 Reply Last reply
        0
        • K kook

          @jsulm my promblem is i am able to check the child item
          but i want to check alll the child items just by checking the root item

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

          @kook So, did you check https://doc.qt.io/qt-5/qtreewidgetitemiterator.html ? It shows how to iterate over child elements...

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

          K 1 Reply Last reply
          2
          • jsulmJ jsulm

            @kook So, did you check https://doc.qt.io/qt-5/qtreewidgetitemiterator.html ? It shows how to iterate over child elements...

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

            @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 1 Reply Last reply
            0
            • 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

                                          • Login

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