Op 1-7-2012 21:50, K. Frank schreef:
> Hi Sean!
>
> Thank you for the suggestion.
>
> On Sun, Jul 1, 2012 at 3:11 PM, Sean Harmer<s...@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?)
>
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

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.

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).

André

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to