0

Is it possible to "lock" selection in QAbstractView?

I have a form with:

  • A list of objects I can edit (QTableView).
    Objects are stored in a database, the list shows their ID.
  • A New button, whose effect is to append an empty row I can edit to create a new ID and enable controls in the rest of the form.
  • An Edit button whose effect is to enable the edition of the rest of the form, but leaves the ID non editable.
  • Everything is driven by a state machine. There are parallel states but for the sake of this question, only transition between a Viewing state and an Editing state is relevant. Clicking either button enters the Editing state, which means testing if I am in the Editing state provides no information about whether I clicked on New or on Edit.

I am trying to lock the selection when entering the Editing state.
I cannot simply disable the view because I need to edit new IDs, I cannot connect a slot to the selectionChanged signal to restore the selection because of side effects (calls to the database + focus going all over the place), and if possible, I wish to avoid having to call QAbstractItemView::setSelectionModel (it is reset by QAbstractItemView::setModel, see below) and wish to drive this behavior thanks only to the Viewing and Editing states.

I have tried to use, but to no avail:

  • QAbstractItemModel::flags (the best I can do is prevent selecting another item but not clearing the selection.)
  • QAbstractItemView::selectionMode
  • QItemSelectionModel::select, with QItemSelectionModel::SelectionFlag::NoUpdate (The description of this enum value made me think it could block the next selection change but that is not the case).
  • Subclassing QItemSelectionModel (@chehrlic's comment below) to override all the virtual public slots.
    While it does work (a property can be used to stop the selection from changing), it is a pain that QAbstractItemView::setModel creates a new model. It does work now but I do not see any easy way to prevent the code from breaking after accumulating code changes over the span of several years.

Did I miss any existing property to achieve this? And if I did not, how can I implement a property to lock the selection but no have any other effect on my view?

At this point, the last item in the above list of things I tried does do the job but it has a downside I am trying to avoid.

This is similar to this question except it was for a now old version of Qt and the answer is not very satisfying anyway.

Atmo
  • 2,281
  • 1
  • 2
  • 21
  • Did not tried it out but create a custom selection model derived from [QItemSelectionModel](https://doc.qt.io/qt-6/qitemselectionmodel.html) and override the select() functions so they don't do anything when you're in edit mode. – chehrlic Dec 21 '22 at 06:43
  • @chehrlic Well, yes subclassing `QItemSelection` model does the trick, that is the best among the options I have so far; I would not qualify that as an ideal solution though: `QAbstractItemView::setModel` creates a new selection model, which means the code could break down by just moving the `view->setModel(...)` for any reason in a few years time. Had `QItemSelectionModel` come with a clone method, the views could create a selection model of the right type; that is not the case though. – Atmo Dec 22 '22 at 05:00
  • I will edit the question and answer my own question if I find a better way some day, unless someone else has a solution first; wait and see. – Atmo Dec 22 '22 at 05:01

0 Answers0