Double the column in QSqlTableModel



  • Hi , can you help me double the coumn in QSqlTableModel.

    [code]
    QTableView table;
    QSqlTableModel model;

    model.setTable("User");
    model.select();
    
    table.setModel(&model);
    
    table.show();
    

    [/code]

    Table "User" have 2 column (Id, name) how i can set in my model to have 3 column (id,id, name)
    coumn id is double

    Its can by in QSqlTableModel or QSqlRelationalTableModel.

    [edit: fixed typo in title, eddy]



  • I'm not quite sure what you mean by "column id is double". Can you show you SQL table structure and exactly what you want please?

    It may be that you need to use QSqlQueryModel instead of QSqlTableModel.



  • its simple.
    I have 1 table user (id, name)
    i create QSqlRelationalTableModel and QSqlRelationalTableModel::setTable (user).
    Now my model have (id,name).
    To add relationships i need 1 colum , but i want add 2 relationships so i need 2 column id (id (relationships), id(relationships), name)



  • So in that case you must have additional tables in order to make relationships to them but I think I see what you are wanting now.

    It looks like QSqlRelationalTableModel will not do what you want on it's own as it only uses the columns in the table that are already present. One way around this would be to create a view in your database that contains the replicated id column and then use this in your QSqlRelationalDatabaseModel.



  • ok, but if i make view in my table and conect QSqlRelationalTableModel it will by editable ??



  • Ah you never said you wanted it to be editable. In that case, you will likely be better off subclassing from QSqlQueryModel to make that editable. Then you can pass in an arbitrary query but it is down to you to do the "right thing" when the user edits the table.



  • I make View , and QSqlRelationalTableModel::SetTable(This_View), and its work ;) its editable ;) thx for help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.