[Interest] How to make a column of a QTableView read-only?

K. Frank kfrank29.c at gmail.com
Sun Jul 1 21:50:54 CEST 2012


Hi Sean!

Thank you for the suggestion.

On Sun, Jul 1, 2012 at 3:11 PM, Sean Harmer <sh at theharmers.co.uk> wrote:
> Hi K. Frank,
>
> On 01/07/2012 19:50, K. Frank wrote:
>> Hello List!
>>
>> I have a QTableView (backed by a QSqlTableModel) and am looking
>> for a simple way to make a column read-only.  Poking around in the
>> documentation, I don't see anything obviously simple.
>
> Take a look at the function QAbstractItemModel::flags(). You can
> override this in your model such that flags it returns do not include
> Qt::ItemIsEditable for the column you wish to be read-only.

To make sure I understand:

In my concrete case, I would then derive from QSqlTableModel,
override its flags() member function, and add to the derived class
some kind of setColumnReadonly() member function (or add some
kind of readonlyColumnIndex to its constructor) to tell flags() which
columns to flag as read-only.  Is that the idea?

Also, do i take it correctly from your answer that there isn't another
way that is clearly simpler?

For me, this approach is fine.  But, as an aside, read-only can be
either a model attribute (if the model is read-only or const) or a view
attribute (if you have a read-only view into a non-read-only model).

In my case, I have only one view into the model, so I can use your
suggested approach to make (a column of) the model read-only.
But, for future reference, what's the best way to make the view
read-only?  (For example, what if my application has two views
into the same model, only one of which should be read-only?)

> Sean

Thanks for any additional thoughts.


K. Frank



More information about the Interest mailing list