Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
I have a QSqlRelationalTableModel which read data from a sqlite database. This model is used in a QTableView widget on my main window to view and edit data. I would like to do two things:
To set the items horizontal alignement to centered on some columns (for program aesthetics),
To enable items edit only on some of the table columns.
I searched Qt documentation following all object hierachies and got nothing for both of the subjects.
Thanks for your help!
andre last edited by
Easiest is to use a proxy model. You can subclass [[doc:QIdentityProxyModel]] (if you're on 4.8) or [[doc::QSortFilterProxyModel]] (for lower versions). There, you reimplement the data() method to return the Qt::AlignHCenter for the Qt::TextAlignmentRole. For all other roles, or for columns you don't want to center, you simply return QIdentityProxyModel::data().
For the read-only columns, you will have to reimplement the flags() method as well.
There are "examples":http://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns on the wiki.
Thanks a lot Andre for your rapid answer! I thought I had the last version but not, I am using 4.7.4. I will subclass QSortFilterProxyModel as you just explained for both functionallities.
Anyway, what I ask to do is very simple and I think I am not the only one to need it. I don't understand why this function is not already inside the Qt library, maybe implemented directly on the model/view classes.
For those who want to solve the same problem, I have written a Wiki page with the code snippet "link":https://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_text_alignment_-and_readonly_columns.
Thanks again Andre for the solution proposal. I hope the code I have written can evolve to some more complete class which can satisfy every user. Feel free to edit this Wiki page! (which by the way I don't know if it is possible)