c++ - QTreeView disable selection on some rows -
i have json
model, , populate qtreeview
:
*-group1 | | | *-item1 value1 | | | *-item2 value2 | *-group2 | *-item4 value3
now want disable selection groups
, user can select rows items
. , want achive without modification of model.
use proxy model such qidentityproxymodel , reimplement qabstractitemmodel::flags(), removing qt::itemisselectable flag group items:
qt::itemflags disablegroupproxymodel::flags(const qmodelindex& index) const { const auto flags = qidentityproxymodel::flags(index); if (index group) { return flags & ~qt::itemisselectable; } return flags; }
then set original (unmodified) model source model of proxy model , proxy model instance tree view’s model:
disablegroupproxymodel* proxy = new disablegroupproxymodel(this); proxy->setsourcemodel(originalmodel); treeview->setmodel(proxy);
Comments
Post a Comment