PDA

View Full Version : Custom selection actions in QTreeView



iraytrace
11th November 2009, 20:40
I have a QTreeView. I'd like to alter the way selection works. When a user selects an intermediate node, I need to select all subordinate nodes as well, and possibly change selection status of superior nodes in the tree (data or selection status).

I've tried working with the selectionChanged signal, but that induces some undesirable feedback (selection changes at this stage cause another iteration of event loop processing, and it can be difficult to differentiate user vs. programmatic changes in subsequent invocation of the selectionChanged signal). I'd like to have a method that gets called *before* anything is selected, so that I can decide what (if anything) gets selected/deselected. Is this possible?

Should I subclass QTreeView and write my own selectionCommand() method? The (minimal) documentation for this hints at doing this to "define your own selection behavior" but I'm not sure if that really means "define your own QAbstractItemView::SelectionBehavior" which isn't what I really want.

What's the right way to do this?

Lykurg
11th November 2009, 20:49
If you are in you slot, called by the selectionChanged singnal, you could use
QObject::disconnect() or QObject::blockSignals(). After alter your selection unblock or reconnect the signal.

squidge
11th November 2009, 20:51
How about subclassing QItemSelectionModel and then using QTreeView->setSelectionModel(&myselectionmodel) ? As far as I'm aware, thats responsible for all selection actions - its what keeps the selection status of all the items. The 'select' call will be called to actually do the select, so you can decide whether or not you want the item selected, and whether or not to select or items as a result.