Results 1 to 4 of 4

Thread: QSqlQueryModel as return value

  1. #1
    Join Date
    May 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QSqlQueryModel as return value

    Hi all,
    I try to write a function, which generates a QSqlQueryModel and returns this model to the calling function. First I thought, this should be an easy thing but obviously I was wrong!
    I searched the forum but found no information, which could help me...

    My function:
    Qt Code:
    1. QSqlQueryModel DBManager::loadConnectionData()
    2. {
    3. QSqlQuery *qry = new QSqlQuery(dbSQLite);
    4. qry->prepare("select * from tbl_connections");
    5. qry->exec();
    6. model.setQuery(*qry);
    7. return model;
    8. }
    To copy to clipboard, switch view to plain text mode 

    When I try to compile the code I get following error message:
    Fehler: C2280: 'QSqlQueryModel::QSqlQueryModel(const QSqlQueryModel &)': attempting to reference a deleted function

    I didn't find an explanation on the net which helped me to understand the problem, so maybe someone here is so kind to help me a little bit?

    Thanks in advance!!

    Viktor

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlQueryModel as return value

    You cannot create a QSqlQueryModel on the stack and return it as the return value of a function. The class has neither a copy constructor nor an assignment operator, one of which would be required to accomplish this. Furthermore, your QSqlQuery is a memory leak. You might have more luck using something like this:

    Qt Code:
    1. QSqlQueryModel * DBManager::loadConnectionData()
    2. {
    3. QSqlQuery qry(dbSQLite);
    4. QSqlQueryModel * model = new QSqlQueryModel( this );
    5. qry,prepare("select * from tbl_connections");
    6. qry.exec();
    7. model->setQuery(qry);
    8. return model;
    9. }
    To copy to clipboard, switch view to plain text mode 

    You probably also need a ";" at the end of your select statement for proper syntax. Remember to call delete on the "model" instance after you are done with it to avoid a memory leak on that.
    Last edited by d_stranz; 16th November 2016 at 01:02.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQueryModel as return value

    Or instead if fiddling with the transient QSqlQuery here use the setQuery() overload that takes a query string and database connection.

    Cheers,
    _

  4. #4
    Join Date
    May 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQueryModel as return value

    thanks for your help, now it works!

Similar Threads

  1. function to return QSQLquerymodel
    By cpuinthebrain in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2015, 12:12
  2. QSqlQueryModel ‘s method rowcount() return 256
    By liuqin820222 in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2013, 09:16
  3. Replies: 1
    Last Post: 2nd January 2013, 09:48
  4. QSqlQueryModel
    By StefanLatsch in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 11:09
  5. Does 'QSqlQueryModel' have a bug?
    By nuntawat in forum Qt Programming
    Replies: 8
    Last Post: 6th April 2010, 17:45

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.