ecatmur
ecatmur

Reputation: 157334

How can I make clicking on a QTreeView partially checked node uncheck the node?

I have a QTreeView populated by a QAbstractItemModel subclass, allowing leaf nodes to be checked by the user. If a proper subset of the descendant leaf nodes of a non-leaf node are checked, then that node is partially checked (Qt::CheckStateRole is Qt::PartiallyChecked).

Currently if the user clicks on a partially checked node then the check state changes to Qt::Checked; I want it to clear the node instead (Qt::Unchecked). Is there a way to customise this behaviour? To control this with a QCheckBox I would override QAbstractButton::nextCheckState(), but I can't find anything similar for QTreeView.

Upvotes: 1

Views: 2503

Answers (1)

trompa
trompa

Reputation: 2007

Implement desired behaviour in you model setData

bool QAbstractItemModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )

For Qt::CheckStateRole

So when you receive in value a Qt::PartiallyChecked. You traverse item childs to set to Qt::Unchecked, and also change current item.

Upvotes: 1

Related Questions