Reordered table model

  • Hello,

    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 :
    Ingredient :
    1 : sugar
    2 : flour
    3 : water

    Dessert :
    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.

    Best regards,


  • Lifetime Qt Champion


    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 ?


  • Lifetime Qt Champion

    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.

    Thank you.

  • Lifetime Qt Champion

    You would likely need a proxy model on top of your QSqlRelationalTableModel in order to move your indexes around without doing special queries.

  • @SGaist Hi !
    Sorry to wake up only now... QAbstractProxyModels are very powerful objects !
    Thank you for your answer, it was exactly what I needed.

Log in to reply