QTreeView disables selection in some rows

I have a model JSONand I fill it out QTreeView:

*-group1
| |  
| *-item1     value1
| |
| *-item2     value2
|
*-group2
  |
  *-item4     value3

Now I want to turn off selection for groupsso that the user can select only rows with items. And I want to achieve it without modifying the model.

+4
source share
2 answers

Use a proxy model such as QIdentityProxyModel and override QAbstractItemModel :: flags () by removing the Qt :: ItemIsSelectable flag for group members:

Qt::ItemFlags DisableGroupProxyModel::flags(const QModelIndex& index) const {
   const auto flags = QIdentityProxyModel::flags(index);
   if (index is group) {
       return flags & ~Qt::ItemIsSelectable;
   }

   return flags;
}

() - - :

DisableGroupProxyModel* proxy = new DisableGroupProxyModel(this);
proxy->setSourceModel(originalModel);
treeView->setModel(proxy);
+1

QItemSelectionModel.

treeView->selectionModel();

void currentRowChanged(const QModelIndex &current, const QModelIndex &previous)

, , previous .

+1

Source: https://habr.com/ru/post/1626195/


All Articles