PDA

View Full Version : Problem with QSqlRelationalTableModel and displaying the data



blups0r
8th June 2011, 23:18
Hey there,

I've got an issue with QSqlRelationalTableModel. I have an PostgreSql database with some tables like Customer and other related stuff. In the table Customer i have foreign keys to other tables. But this keys are not always set with valid values. Now when i use QSqlRelationalTableModel->setRelation and execute the select method i dont get all the entrys in the table. Is there a way to get all the entrys with the select method even though not all foreign keys are set?

I have thought of subclassing QSqlRelationalTableModel or QSqlQuery or QSqlTableModel is this the right approach or should i take a other way? The other Problem i've got is that the data which is stored in the model is more than i want to display. Is there a way to display only the collums i want? Lets assume the table customer has the collums firstname, lastname and age. Now i want the view that it only displays the firstname. The other values should be in the model but not visible to the user. How can i achieve this? Do i have to subclass the QAbstractItemView or is there another way? Maybe subclassing QSqlRelationalDelegate?

I hope you can help me.

Thanks in addvance

jkurdel
9th June 2011, 00:26
For second problem (I assume that you're using QTableView) the solution is to use hideColumn method:
http://doc.qt.nokia.com/latest/qtableview.html#hideColumn

Lesiok
9th June 2011, 08:38
Bad news : QSqlRelationalTableModel is as is.
Problem is with SQL query generated by QSqlRelationalTableModel. This query contains condition

WHERE main_table_id = related_table_idOf course when related_table_id is null the condition is false and result don't contains this records from main table. By me this is the biggest defect of QSqlRelationalTableModel. So You must create Yours own model starting from QAbstractTableModel or QSqlTableModel.