views:

25

answers:

1

Here's my use case (very simplified).

I have some data from DB in QSQLTableModel and I need to transform it: merge few fields into one (and display as such) or split one field into few. How and where this should be done in Model/View?

Notes:

I tried using AbstractProxyModel to do this, but I guess, it's suitable only for filtering or sorting. I could allocate new data and return QModelIndex (which carries the pointer to the data) but which object should deallocate the data later?

Modifying the View object also wouldn't help because it processes every table cell separately.

Mind that I can't alter database in any way. How to do this in Qt? I already spend two whole days on this only to run into one wall after another.

A: 

Using QAbstractProxyModel isn't the wrong approach, you can use it to map any kind of source model to restructure the data. But it will be a lot of work, you have to re-implement several methods, including columnCount, data, flags, index, and more. In effect, you have to make sure that the indexes of this model map to the correct data of the source model. Also, if you have a dynamic model, then you will want to handle signals coming from the source model, modify the values and re-emit them. If you want the view to edit the data as well, then you'll have to re-implement setData, etc.

For example, if you have a column that has two values appended, and you want to display them in two separate columns, then columnCount should return one extra, data should retrieve the original data and return only one part based on the column in the index, index must be modified to check the new bounds of this model, etc.

If you want more specific help with this, then please post some example code.

Ivo