April 29, 2012

Pablik2005 Pablik2005
Lab Rat
43 posts

Double the column in QSqlTableModel

 

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

  1.     QTableView table;
  2.     QSqlTableModel model;
  3.    
  4.     model.setTable("User");
  5.     model.select();
  6.    
  7.     table.setModel(&model);
  8.  
  9.     table.show();

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]

6 replies

April 29, 2012

ZapB ZapB
Ant Farmer
1427 posts

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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 29, 2012

Pablik2005 Pablik2005
Lab Rat
43 posts

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)

April 29, 2012

ZapB ZapB
Ant Farmer
1427 posts

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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 30, 2012

Pablik2005 Pablik2005
Lab Rat
43 posts

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

April 30, 2012

ZapB ZapB
Ant Farmer
1427 posts

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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 30, 2012

Pablik2005 Pablik2005
Lab Rat
43 posts

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

 
  ‹‹ [Solved] questions about an example of "C++ GUI Programming with Qt 4 (2nd Edition)"      QFtp problem ››

You must log in to post a reply. Not a member yet? Register here!