Results 1 to 4 of 4

Thread: QSqlTableModel doesn't works as an object, but works as a pointer

  1. #1
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlTableModel doesn't works as an object, but works as a pointer

    My colleague found a next problem with QSqlTableModel:
    Qt Code:
    1. #define VARIANT_1
    2. class SomeClass : public QWidget
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7.  
    8. SomeClass(QWidget *parent = 0, Qt::WFlags flags = 0)
    9. {
    10. ui.setupUi(this);
    11.  
    12. db = QSqlDatabase::addDatabase("QSQLITE");
    13. db.setDatabaseName(":memory:");
    14. if (!db.open()) {
    15. return;
    16. }
    17.  
    18. QSqlQuery query;
    19. query.exec("create table person (id int primary key, "
    20. "firstname varchar(20), lastname varchar(20))");
    21. query.exec("insert into person values(101, 'Danny', 'Young')");
    22. query.exec("insert into person values(102, 'Christine', 'Holand')");
    23.  
    24. #if defined( VARIANT_1 )
    25. p_model = new QSqlTableModel( );
    26. QSqlTableModel & model = *p_model;
    27. #endif
    28. model.setTable("person");
    29. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    30. model.select();
    31.  
    32. model.setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    33. model.setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
    34. model.setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
    35.  
    36. ui.tableView->setModel(&model);
    37.  
    38. }
    39.  
    40. private:
    41. Ui::qhlbClass ui;
    42. #if defined( VARIANT_1 )
    43. QSqlTableModel * p_model;
    44. #else
    45. #endif
    46. };
    To copy to clipboard, switch view to plain text mode 

    if VARIANT_1 is defined tableView shows a right table, else - not.
    I.e. if QSqlTableModel comes as an object table doesn't shows it's data...
    If QSqlTableModel comes as a pointer it's works fine...
    Can you comment this ?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,693
    Thanks
    259
    Thanked 752 Times in 742 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QSqlTableModel doesn't works as an object, but works as a pointer

    Where have you created an association between your db and your query? You aren't checking the return value of your query exec statements, so you have no idea if they are doing anything at all.

    Likewise, where do you create an association between your db and the model? You don't check the return value from select() either, so you don't know that it has failed.

    You basically have three totally independent things here, none of them know anything about the others, so it is no wonder that nothing shows up in the view.

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel doesn't works as an object, but works as a pointer

    d_stranz, you are partially right. QSqlTableModel needs a QSqlDatabase in a constructor and uses "default" one if I don't give it. In case of object (not pointer) QSqlTableModel's constructor calling before addDatabase function call, but in case of pointer default db exists. Here is a full answer
    I didn't check exec() and select() results to reduce code exceptionally. I check it in "urgent" project, certainly
    Thank you.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,671
    Thanks
    13
    Thanked 1,595 Times in 1,523 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel doesn't works as an object, but works as a pointer

    Similar discussion from a couple of days ago.

Similar Threads

  1. Replies: 15
    Last Post: 8th June 2013, 07:15
  2. Replies: 2
    Last Post: 21st March 2011, 23:00
  3. Replies: 1
    Last Post: 31st December 2010, 17:35
  4. wince project doesn't work on wince, but works on win mobile
    By thedimitrius in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 29th March 2010, 13:01
  5. Replies: 0
    Last Post: 16th July 2009, 16:58

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.