Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Reordered table model
I have to create a widget to administrate (read + write) a sqlite database with the following structure :
idIngredient | idDessert | Quantity
1 | 10 | 3
2 | 11 | 8
3 | 10 | 9
1 | 11 | 7
Here, idIngredient and idDessert are foreign Keys which reference other tables. Let say :
1 : sugar
2 : flour
3 : water
10 : Pie
11 : Cake
I'm would like to display them in a crossed table :
...........|..Sugar... |..Flour.. | Water
Pie.... |... 3......... |.............. |.... 9.....
Cake..|... 7......... |....... 8... |............
Can I do that simply using existing Qt sql model tools (relational ? Or others) ? (which would avoir me to write update queries...). If not, how can i do that ?
I imagine I'm not the only one with this need.
Thank you in advance for your help.
What size should the cake be ? ;)
@SGaist Hi ! Are you serious ? What do you mean by "size"?
I think i could do it by writing 2 table models :
- 1 Qsqlrelationaltablemodel which performs queries on the raw table
- 1 simple QStandardItemModel which has the previous sql model as a member and the right number of row and cols (returned by sql model). I can overload setData method to invoke Qsqlrelationaltablemodel::setData on the right index.
Isn't it a bit complicated ? Is there a simplest way to do that ?
I meant how big would the resulting cake be ;-)
So you would like to have both header views matching the content of the ingredient and dessert table while the content itself would be the table of quantity of each ingredient by desert, right ?
@SGaist Yes, it's exactly that : both header views should match the number of entries of Ingredient and Dessert tables. Number of dessert and Ingredient should contain fewer than 5-6 different entries, so this would be more readable than displaying the original raw table.
sebolinho61 last edited by
@SGaist Hi !
Sorry to wake up only now... QAbstractProxyModels are very powerful objects !
Thank you for your answer, it was exactly what I needed.