0

I use Qt 5.6 and a QStandardItemModel filled with QStandardItem to display a data table.

Some items are enabled, some are not.

Only enabled items can be selected (this is the normal behavior).

But when the data are updated, some items enabled state can change, and this is the source of my problem. Consider the following scenario :

1 an item is enabled
2 the user select it
3 the item is selected
4 the data are updated, the item is now disabled
5 the item remain selected, despite it is disabled

And now, the user can interact with a disabled item : open context menu, edit it...

It there some "update" method in Qt model/view framework that I can call to update the selected state after a flag change ?

Or is there any simple (and generic) way to solve this problem, for all my item views ? (not all are tables, not all are based on QStandardItem)

Thanks

Aurelien
  • 1,032
  • 2
  • 10
  • 24

1 Answers1

0

EDIT: This does not seem to work as the enabled property is no data but a flag and editing this should not trigger the named signal. Hang on!

What about connecting the signal

void QStandardItemModel:itemChanged(QStandardItem * item)

with some helper code like

void fixSelection(QStandardItem * item)
{
    if (item.isEnabled()) {
        // remove item from selection model of view...
    }
}

See also here for some information about QItemSelectionModel.

You may also think about clearing the whole selection of the model if the data is modified. This will save you this extra amount of work. I do not know which action might disable some items under selection and which followup action which wants to work on the previously taken selection.

maxik
  • 1,053
  • 13
  • 34
  • I want to keep the current selection (for the items that are still enabled), that's the problem ! That data change, but only the values, and it realy make sense for the user to preserve the current selection. – Aurelien Jun 30 '16 at 09:33
  • @Aurelien So does the *enabled* property depends on a data change? Then the above solution might still work. Otherwise you have to toggle the `fixSelection` method yourself, which should work for you. – maxik Jun 30 '16 at 09:38
  • Yes, the enabled property depends on data change. I can't really use your solution directly, because of 2 reasons : 1/ with a large number of items, there is a performance hit 2/ it doesn't work with non-QStandardItem based models. But I think I'll end up with a similar implementation – Aurelien Jun 30 '16 at 12:29
  • @Aurelien To 1/ if the changed item is the item which was disabled then the performance hit should be small, because you have the item from the signal already. Only browse all selected items and find it. If selection is big then this problem is real anyway. To 2/ you might consider `void QAbstractItemModel::dataChanged`. – maxik Jun 30 '16 at 12:37