Results 1 to 4 of 4

Thread: QSqlRelationalTableModel setQuery

  1. #1
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QSqlRelationalTableModel setQuery

    Hello,

    According to the documentation found here:

    http://doc.trolltech.com/4.3/qsqlque...blic-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.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel setQuery

    setQuery() method is public in QSqlQueryModel. In QSqlTableModel and QSqlRelationalTableModel this method is protected.
    Please read documentation carefully.

  3. #3
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlRelationalTableModel setQuery

    Click this link:

    http://doc.trolltech.com/4.4/qsqlrel...l-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.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlRelationalTableModel setQuery

    Quote Originally Posted by RolandHughes View Post
    Click this link:

    http://doc.trolltech.com/4.4/qsqlrel...l-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.

Similar Threads

  1. QSqlQueryModel setQuery problem
    By cyberboy in forum Qt Programming
    Replies: 14
    Last Post: 14th March 2008, 00:07
  2. Subclassing QSqlTableModel to overwrite setQuery
    By montuno in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2007, 10:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.