PDA

View Full Version : QSqlRelationalTableModel setQuery



RolandHughes
13th November 2008, 22:48
Hello,

According to the documentation found here:

http://doc.trolltech.com/4.3/qsqlquerymodel.html#public-functions

I should be able to do the following:

QSqlDatabase db = QSqlDatabase::database("xpns");
tableModel = new QSqlRelationalTableModel(this, db);
tableModel->setTable("expenses");
tableModel->setRelation(tableModel->fieldIndex("category"),
QSqlRelation("categories", "category", "category"));
tableModel->setRelation(tableModel->fieldIndex("payee"),
QSqlRelation("payees", "payee", "payee"));
tableModel->setSort(tableModel->fieldIndex("tran_dt"), Qt::AscendingOrder);
tableModel->setQuery( QSqlQuery("SELECT tran_dt, category, tax_ded, payee, amount FROM expenses;", db));
tableModel->select();


You get the following error when you compile:

/usr/include/qt4/QtSql/qsqltablemodel.h: In constructor ‘XpnsForm::XpnsForm(int, int, QWidget*)’:
/usr/include/qt4/QtSql/qsqltablemodel.h:129: error: ‘void QSqlTableModel::setQuery(const QSqlQuery&)’ is protected
xpnsform.cpp:89: error: within this context


Is this just a documentation bug? I need to exclude a serial key since the data mapper appears to be trying to update it. If I take the tran_id column off the table things appear to work, so, I was looking for a simple way to exclude it from the select.

Lesiok
14th November 2008, 13:34
setQuery() method is public in QSqlQueryModel. In QSqlTableModel and QSqlRelationalTableModel this method is protected.
Please read documentation carefully.

RolandHughes
14th November 2008, 18:13
Click this link:

http://doc.trolltech.com/4.4/qsqlrelationaltablemodel-members.html

then click on the setQuery ( const QString & query, const QSqlDatabase & db = QSqlDatabase() ) entry in the list.

It is not flagged as protected. setQuery( QString & query) is flagged protected.

I followed the doc carefully. The doc on-line is wrong or the developer didn't read it before coding.

Making this method protected well and truly pooched the functionality of the class. The class doesn't handled serial database generated ID columns for real databases that actually enforce rules. The only "work around" is to change the query string used by the class so that column can be skipped. Now the "work around" is locked away.

I'm starting to understand why they took the database integration out of the designer. It must have had a LOT of issues.

Lesiok
15th November 2008, 06:59
Click this link:

http://doc.trolltech.com/4.4/qsqlrelationaltablemodel-members.html

then click on the setQuery ( const QString & query, const QSqlDatabase & db = QSqlDatabase() ) entry in the list.

It is not flagged as protected. setQuery( QString & query) is flagged protected.

Yes, you are right. I think that this an error in doc and in implementation. Quotation from comments to QSqlTableModel::setQuery( const QSqlQuery & query ) method in qsqltablemodel.cpp : You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.