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. QTreeWidget QCheckBox signal itemChanged for root
Qt 6.11 is out! See what's new in the release blog

QTreeWidget QCheckBox signal itemChanged for root

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 868 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.
  • L Offline
    L Offline
    Lior
    wrote on last edited by
    #1

    Hi,
    I'm want to implement tree of checkbox.
    I used tree widget.

    when some root is change, I want to update all the childes,
    but when I update some child check state, the signal fire again.

    when one child is changed , it work partly ok, when i need to change to root (let say if all is selected and i uncheck one child so i need to uncheck the root and i got the same problem)

    is there is a good example or explain a good way to implement it??

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      Updating the check state will always fire the signal, unless you block signals before programmatic update and restore the original state afterwards. You can do that manually or with a signal blocker, see here.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      2
      • L Offline
        L Offline
        Lior
        wrote on last edited by
        #3

        @Axel-Spoerl said in QTreeWidget QCheckBox signal itemChanged for root:

        u can do that m

        can you offer a solution to this fetcher? (tree of checkboxes)
        when you check/uncheck root - update all the children as the root
        if a child is checked and it's the last that not checked - set the root as checked (and the same logic for unchecking).

        Axel SpoerlA 1 Reply Last reply
        0
        • L Lior

          @Axel-Spoerl said in QTreeWidget QCheckBox signal itemChanged for root:

          u can do that m

          can you offer a solution to this fetcher? (tree of checkboxes)
          when you check/uncheck root - update all the children as the root
          if a child is checked and it's the last that not checked - set the root as checked (and the same logic for unchecking).

          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          @Lior
          What do you mean by „this fetcher“?
          Post your code and the community will look what can be improved.

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lior
            wrote on last edited by Lior
            #5

            Hi,
            Here is the example code. it is quick and dirty only for learning this, I will rewrite it,
            the main objective is to hold a tree of checkboxes,

            1. when a checkbox is checked or unchecked - do something
            2. if a root is checked or unchecked - update all the children
            3. if all the children are checked, update the root to be checked
            4. if at least one of the children is not checked, the root is not checked
            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QPushButton>
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                ui->treeWidget->setColumnCount(1);
            
                // set headers
                QStringList label ;
                label << "FileName";//<< "FilePath"<< "Widget";
                ui->treeWidget->setHeaderLabels(label);
            
                QTreeWidgetItem *root = new QTreeWidgetItem(ui->treeWidget);
                root->setText(0,"RootFile");
                root->setCheckState(0, Qt::Unchecked);
                ui->treeWidget->addTopLevelItem(root);
            
                addItem(0, "c" + QString::number(1), Qt::Unchecked, root);
                addItem(0, "c" + QString::number(2), Qt::Unchecked, root);
                addItem(0, "c" + QString::number(3), Qt::Unchecked, root);
                addItem(0, "c" + QString::number(4), Qt::Unchecked, root);
            
            }
            
            void MainWindow::addItem(int col, QString name, Qt::CheckState checkedState, QTreeWidgetItem *root)
            {
                CPloyPropertiesTreeWidgetItem *child = new CPloyPropertiesTreeWidgetItem();
                child->setText(col,name);
                child->setCheckState(col, checkedState);
                root->addChild(child);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            
            void MainWindow::on_treeWidget_itemChanged(QTreeWidgetItem *item, int column)
            {
                bool isRootChanged = item->parent() == nullptr;
            
                if(!isRootChanged)
                {
                    Qt::CheckState cs = item->checkState(column);
                    QTreeWidgetItemIterator it(item->parent());
                    bool isAll = true;
                    ++it; // skip the root
                    while (*it)
                    {
                        // if at least one is different, uncheck the root, otherwise set the root as the child
                        if ((*it)->checkState(column) != cs)
                        {
                            isAll = false;
                            break;
                        }
                        ++it;
                    }
            
                    if(isAll)
                    {
                        item->parent()->setCheckState(column,cs);
                    }
                    else
                    {
                        item->parent()->setCheckState(column,Qt::CheckState::Unchecked);
                    }
                }
            
                if(isRootChanged)
                {
                    QTreeWidgetItemIterator it(item);
                    Qt::CheckState rootCs = item->checkState(column);
                    ++it;
                    while (*it)
                    {
                        if ((*it)->checkState(column) != rootCs)
                        {
                            (*it)->setCheckState(column, rootCs);
            
                        }
                        ++it;
                    }
                }
                else // child changed
                {
                   enableDisableVisablity();
                }
            }
            
            void MainWindow::enableDisableVisablity()
            {
            
            }
            
            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