Qt World Summit: Submit your Presentation

Qt::ItemIsSelectable and QTreeView

  • After playing around with the following function:

    virtual Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex& index) const;

    as expected, not returning Qt::ItemIsSelectable prevents the item from being selected/unselected, but it also gets removed from the selectionModel().selectedIndicies() list even if it was last known to be selected. There is never a notification of a selection change for that item, so it still appears as selected.

    I would think that not returning Qt::ItemIsSelectable would only prevent an item from changing it's selection state, not completely removing all selection related functionality.

    The use case here is that I would like to have items in a tree view that are in a "locked" state where selection cannot be changed, but are still properly reported as selected by the model.

    I am currently working around this by allowing a selection change but immediately changing it right back, which seems to function without visible artifacts but does not seem ideal.

  • Could you post your code?

  • QItemSelectionModel::select() is virtual. Subclassing and overriding select to ignore the items that can't be changed might work.

    edit: 4.5 years later... Reviving long dormant threads seems to be popular.

  • Sounds very similar to QTBUG-88723.

    The code that excludes items that are not selectable and enabled is in indexesFromRange() and it's been there since the trolltech days. You can't use the isSelected() workaround in the bug report either as it only works around the Qt::ItemIsEnabled check so i reckon allowing the change and change it back is the way to go here. Connect a slot to QItemSelectionModel::selectionChanged that reverts the changes you don't want to happen

Log in to reply