Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Qtreeview With CheckBox

    General and Desktop
    2
    6
    18219
    Loading More Posts
    • 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.
    • S
      Shiv last edited by

      Dear All

      I have implemented a QTreeview based on the example provided by qt
      http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html

      Now I want to add check box to all items in the treeview and check which are the items are checked while processing the check state. Any help is appreciated.

      Thanks

      1 Reply Last reply Reply Quote 0
      • Ratzz
        Ratzz last edited by

        Hi,
        Here are some of useful links
        http://stackoverflow.com/questions/8175122/qtreeview-checkboxes
        http://stackoverflow.com/questions/14158191/qt-qtreeview-and-custom-model-with-checkbox-columns

        --Alles ist gut.

        1 Reply Last reply Reply Quote 1
        • S
          Shiv last edited by

          Hi @Ratzz
          Thanks for your kind reply.
          The link is useful and somehow i have implemented but i have some, more queries.

          1.Instead of enabling check box to all the items in the treeview is there any option to enable the check box only to certain items.
          2.How to check which are the items are checked in the treeview.

          Thanks

          Ratzz 1 Reply Last reply Reply Quote 0
          • Ratzz
            Ratzz @Shiv last edited by

            @Shiv
            Can you show me how you have done?

            --Alles ist gut.

            1 Reply Last reply Reply Quote 0
            • S
              Shiv last edited by

              Hi

              //data method of TreeModel
              QVariant TreeModel::data(const QModelIndex &index, int role) const
              {
                  if (!index.isValid())
                      return QVariant();
              
                 TreeItem *item1 = static_cast<TreeItem*>(index.internalPointer());
              
                  if ( role == Qt::CheckStateRole && index.column() == 0 )
                  {
                       return static_cast< int >( item1->isChecked() ? Qt::Checked : Qt::Unchecked );
                  }
                  if (role != Qt::DisplayRole && role != Qt::EditRole)
                      return QVariant();
              
                  TreeItem *item = getItem(index);
              
                  return item->data(index.column());
              }
              
              //setData method of TreeModel
              bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
              {
                  TreeItem *item = getItem(index);
                  if(role == Qt::CheckStateRole)
                  {
                              qDebug()<<"Ischecked"<<item->isChecked();
              
                              if(item->isChecked())
                                  item->setChecked(false);
                              else
                                  item->setChecked(true);
                              emit dataChanged(index, index);
                              return true;
                  }
              
                  if (role != Qt::EditRole)
                      return false;
                  bool result = item->setData(index.column(), value);
              
              
                  if (result)
                      emit dataChanged(index, index);
              
                  return result;
              }
              
              
              //flags of TreeModel
              Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
              {
                  if (!index.isValid())
                      return 0;
              
                  return Qt::ItemIsEditable | QAbstractItemModel::flags(index)|Qt::ItemIsUserCheckable;
              }
              
              //SetChecked function for treeitem class
              void setChecked( bool set ){ checked = set; }
              

              Theses are the function I have changed to enable the check boxes from the example given http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html

              Thanks.

              1 Reply Last reply Reply Quote 0
              • S
                Shiv last edited by

                Hi
                Tree model Looks like

                -User //*Parent  // No check box
                .....+UserDept1 //*child  // No check Box
                ........... userName1//*Grandchild  // check box enabled
                ........... username2//*Grandchild  // check box enabled
                ........... userName3//*Grandchild  // check box enabled
                ....+UserDept2 //*child
                ....-USerDept3 //*child
                
                

                Thanks

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post