Results 1 to 9 of 9

Thread: Does 'QSqlQueryModel' have a bug?

  1. #1
    Join Date
    Mar 2010
    Location
    Bangkok, THAILAND
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Does 'QSqlQueryModel' have a bug?

    Hello. Now I have a problem when using QSqlQueryModel or QSqlTableModel. I installed an open-source version of Qt 4.6.2 for VS2008 and Qt 1.4.4 add-in for VS2008 on WinXP. The VC++ compiler always throw the error message "error C2248: 'QSqlTableModel::setQuery' : cannot access protected member declared in class 'QSqlTableModel'" when I set a parameter in setQuery() function as below:
    Qt Code:
    1.  
    2. // assume that 'stock' is a table.
    3. model->setQuery(tr("SELECT * FROM stock WHERE id = %1").arg(ui.searchComboBox->currentText()));
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1.  
    2. // assume that 'stock' is a table.
    3. model->setQuery("SELECT * FROM stock WHERE id = 1");
    To copy to clipboard, switch view to plain text mode 
    But according to a Qt documentation, it said that user can pass a string into setQuery() function. So is it a bug and what should I do?

    Thanks a lot!
    Last edited by nuntawat; 16th March 2010 at 12:57. Reason: Corrent a version number of Qt

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    according to QSqlTableModel::setQuery documentation:


    void QSqlTableModel::setQuery ( const QSqlQuery & query ) [protected]
    This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.

    See also selectStatement().
    try setFilter() on the QSqlTableModel, just like the documentation suggests or use the QSqlQueryModel.

  3. #3
    Join Date
    Mar 2010
    Location
    Bangkok, THAILAND
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    Quote Originally Posted by schnitzel View Post
    according to QSqlTableModel::setQuery documentation:




    try setFilter() on the QSqlTableModel, just like the documentation suggests or use the QSqlQueryModel.
    Sorry, but acrtually I would like to query using either QSqlQueryModel or QSqlTableModel (depending on a situation) from more than one table. So I need to set up the query myself. Does anyone have the solution?
    Last edited by nuntawat; 16th March 2010 at 18:46. Reason: correct a mistake

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    Did you #include <QSqlQueryModel> ? That would give you the same error if you did not.

  5. #5
    Join Date
    Mar 2010
    Location
    Bangkok, THAILAND
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    Quote Originally Posted by waynew View Post
    Did you #include <QSqlQueryModel> ? That would give you the same error if you did not.
    I think the only way to use setQuery() function is passing string directly; noth tr() and QString can't be passed into that function.

    But now I have a problem because I would like to use the QSqlQueryModel to fetch the data with specific condition (WHERE clause) and then display, but I can't add it to the query string which must be passed directly as I told before, and furthermore there is no function like setFilter() as QSqlTableModel has.

    I'm very appreciated if you or any one post a solution. Thanks!!

  6. #6
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    That is not true. You can do this (it works in my code):

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT * from table_name where column = ?");
    3. query.addBindValue("value");
    4. query.exec();
    5. model->setQuery(query);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to waynew for this useful post:

    time (6th December 2016)

  8. #7
    Join Date
    Mar 2010
    Location
    Bangkok, THAILAND
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    Quote Originally Posted by waynew View Post
    That is not true. You can do this (it works in my code):

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT * from table_name where column = ?");
    3. query.addBindValue("value");
    4. query.exec();
    5. model->setQuery(query);
    To copy to clipboard, switch view to plain text mode 
    Thanks!!! I just knew that setQuery() didn't execute the query!!

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Does 'QSqlQueryModel' have a bug?

    Quote Originally Posted by nuntawat View Post
    I think the only way to use setQuery() function is passing string directly; noth tr() and QString can't be passed into that function.
    Hmm? That's not true.

    But now I have a problem because I would like to use the QSqlQueryModel to fetch the data with specific condition (WHERE clause) and then display, but I can't add it to the query string which must be passed directly as I told before, and furthermore there is no function like setFilter() as QSqlTableModel has.
    The table model is for representing a single table, you can't query for data from more than one table using that. You can't use setQuery() on QSqlTableModel as this wouldn't make sense - what differs QSqlTableModel and QSqlQueryModel is the query itself. If you overrode that, the table model could not execute its query anymore, that's why it is protected in QSqlTableModel. setFilter() only sets the WHERE clause on the model's query, it will influence rows, not columns. Functionally the two models are alike with the only exception that the table model is able to insert new rows into the table.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Apr 2010
    Location
    United States
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does 'QSqlQueryModel' have a bug?

    as wysota pointed out what you are attempting conflicts with documentation:

    void QSqlTableModel::setQuery ( const QSqlQuery & query ) [protected]
    This function simply calls QSqlQueryModel::setQuery(query). You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.

    See also selectStatement().
    Not only does setQuery not accept a string it is protected which means unless you make your own class which inherits from QSqlTableModel and sets the query internally this will cause a compile error. Regardless the query needs to be a single table select statement I believe.

    Calling setTable will fetch the column information and setup your SELECT and FROM clauses.
    Calling setFilter will setup your WHERE clause with the predicates you specify.
    Calling setSort will setup your ORDER BY clause with column and ascending or descending keyword.

    The minimum required before calling select() to populate the data is setTable which if used alone will basically do SELECT * FROM tablename except it will explicitly select columns so the order is guaranteed.

Similar Threads

  1. About QSqlQueryModel
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 11th December 2009, 23:27
  2. QSqlQueryModel tweaks?
    By baray98 in forum Qt Programming
    Replies: 5
    Last Post: 22nd September 2009, 01:58
  3. how can I refresh QSqlQueryModel?
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 20th June 2009, 04:39
  4. QTableView/QSqlQueryModel
    By norobro in forum Qt Programming
    Replies: 7
    Last Post: 15th February 2008, 22:52
  5. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 09:55

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.