1

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.

ecatmur
  • 152,476
  • 27
  • 293
  • 366

1 Answers1

1

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.

trompa
  • 1,967
  • 1
  • 18
  • 26
  • 2
    `value` isn't `Qt::PartiallyChecked`, it's `Qt::Checked`. That's the problem. – ecatmur Jan 17 '13 at 17:07
  • So, do it when you receive a `Qt::Checked`. What's the problem? If you need it, you can even access current state with data(index, role) – trompa Jan 17 '13 at 17:16
  • 2
    That doesn't distinguish between `setData` called from a mouse click (or pressing the space bar) and from another cause, where `Qt::Checked` is the wanted value. Hoping there's a better way (what code calls `setData`)? – ecatmur Jan 17 '13 at 17:48
  • 1
    setData is called by delegate. Implementing a custom delegate is the other way to get what you want – trompa Jan 17 '13 at 17:50