Qtreeview With CheckBox
-
Dear All
I have implemented a QTreeview based on the example provided by qt
http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.htmlNow 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
-
-
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
-
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.
-
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