Custom view or delegate?
-
Hi,
I have a QSqlTableModel and I would like to draw each row as depicted in this picture:"name row" is just another column of the table (let's say column0). Each other column will be drawn as a couple "Label: LineEdit" or something similar, it is not very important at this point.
In order to do so, do I need to subclass QAbastractItemView or can I just use an existing View and implement my own delegates? What's the typical workflow in this case?
I know how to customize the rendering of a single item thanks to delegates, but how do I modify the structure which contains the items? Which QAbstractItemView's methods do I need to override?
-
Hi,
I have a QSqlTableModel and I would like to draw each row as depicted in this picture:"name row" is just another column of the table (let's say column0). Each other column will be drawn as a couple "Label: LineEdit" or something similar, it is not very important at this point.
In order to do so, do I need to subclass QAbastractItemView or can I just use an existing View and implement my own delegates? What's the typical workflow in this case?
I know how to customize the rendering of a single item thanks to delegates, but how do I modify the structure which contains the items? Which QAbstractItemView's methods do I need to override?
@tungsteno
This is a good question, to which unfortunately I don't know the answer.It seems to me that you will have a lot to re-implement starting out from
QAbstractItemView
or even your own delegates for this. You also want rows for columns, and I'm not sure whether anything fits nicely for this. If you can do it one of your ways, that's fine, I could be quite wrong.If you cannot, what about the following. It could be way off the wall.... :)
Your layout looks like a
QTreeView
, fixed depth 2, nodes have only one column, leaves have a second column for the value.What about: you create a "proxy" model to sit between your
QSqlTableModel
and yourQTreeView
. It's only job is to map between what the tree view needs as rows and what are actually columns in your SQL model. Override the proxy model'ssetData()
&data()
methods to map the row in the treeview to/from the row, column in the real table model. So theQTreeView
sees a model with rows/columns as you want them on the screen, but you actually store/retrieve values from rows/columns as they are in yourQSqlTableModel
.QSortFilterProxyModel
already containsmapFrom/ToSource()
methods (and that may mean itssetData()
automatically writes to source model for you, I don't know), so I'm thinking you could use that as your "proxy" model.If this works, you should get all editing facilities for the values automatically, with little code because you're starting from existing concrete classes. (You may also get for free folding on each row to show/hide the column list, which could be nice for your interface.) That's my theory anyway.
Like I said, someone else may be able to point you to solving via
QAbstractItemView
/delegates and that may be simpler/cleaner. But I thought about this and that's what I came up with. I shall be following this question!