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. Qtreeview With CheckBox
Forum Updated to NodeBB v4.3 + New Features

Qtreeview With CheckBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 22.3k Views 1 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.
  • S Offline
    S Offline
    Shiv
    wrote on 15 Apr 2016, 07:34 last edited by
    #1

    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
    0
    • R Offline
      R Offline
      Ratzz
      wrote on 15 Apr 2016, 11:17 last edited by
      #2

      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
      1
      • S Offline
        S Offline
        Shiv
        wrote on 18 Apr 2016, 06:29 last edited by
        #3

        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

        R 1 Reply Last reply 18 Apr 2016, 06:38
        0
        • S Shiv
          18 Apr 2016, 06:29

          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

          R Offline
          R Offline
          Ratzz
          wrote on 18 Apr 2016, 06:38 last edited by
          #4

          @Shiv
          Can you show me how you have done?

          --Alles ist gut.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shiv
            wrote on 18 Apr 2016, 06:51 last edited by
            #5

            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
            0
            • S Offline
              S Offline
              Shiv
              wrote on 18 Apr 2016, 06:59 last edited by
              #6

              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
              0

              1/6

              15 Apr 2016, 07:34

              • Login

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