Important: Please read the Qt Code of Conduct -

Select disabled cell in QAbstractItemView

  • Imagine you have a QListView that lists users that are registered at something. Let's say there are two columns - one that contains the full user name and has a checkbox to the left, and another one - the status of the user registration process which is done asynchronously over the network. The first column is fully editable - the checkbox can be checked and unchecked to register or unregister the user and the user name can also be changed. The second one is read only - only displays the registration progress and status. Imagine that while registering a given user some error occurred and it is required that the editable fields are no more editable and to be obvious to the client that they aren't. One possible way is to make the first column uneditable (through the item flags in the underlying model of the view). This prevents the client from editing the user name but the field gives no notice that it has become "uneditable" unless the client tries to edit it and sees that is is impossible. So this UX is not good. Making the cells in the column uneditable doesn't prevent the client from registering/unregistering the user. Making the column not user checkable (through the item flags in the model) is not an option since the view won't show the checkbox to the left of the user name anymore. It is possible to manually simulate the impossibility to edit the checkbox state through the model - if someone tries to change the user registration state while there is an error from previous registration or user name change, we could manually revert this in the model. Again, this is both not good - client has no first impression of that he/she cannot change the registration state, because the UI of the checkbox hasn't changed in any way. Analogously to the behavior of the user name, the client would notice the lack of editability of the checkbox as soon as he/she prompts to change its state. The other way is to disable the whole column - both it would be uneditable and it would be obvious that its state has changed in some way because it would change UI - checkbox icon would be faded, the color of the text of the user name, too. The problem - it would be no more possible to select the row at which the error has happened by clicking on the cell in the first column because of the fact it is uneditable. Clicking on the second column - the one which briefly informs about the error, the whole row becomes selected - even the cell which cannot be selected anymore.

    Question: how to make a given cell both selected and disabled?

    It is impossible through the item flags. By default they supply selected and enabled traits. Removing only the enabled flag doesn't remove the selectable one but views probably consider the enabled too when allowing a given cell to be selected. I simulated selectability of the disabled cell by hooking to the clicked(QModelIndex) signal and programatically selecting the disabled cell but this is not good, since if the selection is an extended one and the client has pressed the CTRL button to add an item to the selection when receiving the clicked(QModelIndex) signal it would be hard to see if the user wanted to add to the selection or to clear it and select only that cell. Yes, it is possible - derive the item view and detect the keys but reinventing the wheel for handling selections is not a good option.

  • Lifetime Qt Champion


    Rather than making something disabled when in fact it shouldn't, why not make your UI rather tell the user something is happening using different colors for your cells ?

Log in to reply