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

K. Frank kfrank29.c at gmail.com
Mon Jul 2 16:18:45 CEST 2012


Hello André!

Thank you for the proxy-model explanation.

On Mon, Jul 2, 2012 at 3:42 AM, André Somers <andre at familiesomers.nl> wrote:
> Op 1-7-2012 21:50, K. Frank schreef:
>> ...
>>>> ...
>>>> I have a QTableView (backed by a QSqlTableModel) and am looking
>>>> for a simple way to make a column read-only.
>>>> ...
>> ...
>> 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?)
>>
> You can use a proxy model to do that. There is a ready-made one that
> does what you want available on the qt-project wiki:
> http://qt-project.org/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns

Thank you.  That's a good reference and a nice utility class.

Also, I think I'm getting a better sense now of how the Qt model
classes fit together.

> If you have multiple views on a single model with different demands for
> what column should be read-only, you can create multiple proxy models on
> the same SQL model.

Yes, this addresses my (hypothetical) multiple-view issue.

> One note on the wiki article: for Qt 4.8 and up, you should replace the
> use of QSortFilterProxyModel with QIdentityProxyModel (as long as you
> don't need filtering or sorting too, that is).

Yes.  (Including those troglodytes, such as myself, who insist upon
using -- shudder -- a Release Candidate, 4.8.0-rc1!)

For the record, in my case I ended up building a read-only delegate
(that overrides createEditor to return NULL).  So far it seems to work
fine.

I did it this way because using the delegate seems more view-ish, and
in my use case I think of the read-only attribute as being attached to
the view.  (However, I don't really feel like I've fully absorbed the Qt
philosophy about where the model leaves off and the view begins.)

Also, the read-only delegate seems to me to be lighter weight (although
without knowing more about what is going on under the Qt hood, I may
be wrong about this).

> André

Thanks again for your help.


K. Frank



More information about the Interest mailing list