[Interest] Revert the selection in a QTreeView

Tony Rietwyk tony at rightsoft.com.au
Wed Jan 14 03:31:27 CET 2015


Glenn wrote:

> Sent: Wednesday, 14 January 2015 8:45 AM
> 
> Hi,
> 
> In a QTreeView how can I revert the selection?
> 
> The behaviour I want is that when the user selects an item, then based on
> some other state, it will either accept the selection or message the user
and
> revert to the previous selection.
> 
> The currentChanged method gives me the QModelIndex of the previous
> selection so I can use treeview.setCurrentIndex to set it to the previous
> index, but this causes recursion.
> 
> I guess I could use a flag to prevent the recursion, but I'm wondering if
there
> is a better way to get the same behaviour.

Hi Glenn, 

Don't worry - you'll probably end up with several of these 'prevent
recursion' flags for your slots!  

Note:  in Qt 4 there is a bug where calling setCurrentItem in the
currentIitemChanged slot after a mouse click may cause all of the items in
between to be selected - at least in does in QListView!  The solution was to
save the previous item, and use QTimer.singleShot( 0, ... ) to a slot that
does setCurrentItem on the saved value. 

Unfortunately, the Qt API doesn't have an interface that allows you to trap
the change in selection regardless of the cause.  I have recently tried
overriding keyboardSearch, moveCursor and mousePressEvent to force the
validation before the selection is changed.  

A similar problem occurs when you need to validate a page in a tab widget
before the tab changes. 

Good Luck! 






More information about the Interest mailing list