Results 1 to 5 of 5

Thread: QSqlTableModel and insertRecord()

  1. #1
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel and insertRecord()

    I'm trying use a Model / View framework. I know how to make model and how to display data with QTableView and how to use QDataWidgetMapper. But I have problem with QDataWidgetMapper when I want to insert new record. Actualy I use something like this:

    Qt Code:
    1. QSqlRecord record = model->record();
    2.  
    3. // append new record
    4. model->insertRecord( -1, record );
    5.  
    6. mapper = new QDataWidgetMapper();
    7. mapper->setModel( _model );
    8. mapper->addMapping( )
    9. mapper->addMapping( )
    10. mapper->addMapping( )
    11.  
    12. // HERE IS MY QUESTION
    13. mapper->setCurrentIndex( model->rowCount() - 1 );
    To copy to clipboard, switch view to plain text mode 

    I use
    Qt Code:
    1. mapper->setCurrentIndex( model->rowCount() - 1 );
    To copy to clipboard, switch view to plain text mode 
    but rowCount doesn't have a true row count. How can I insert a record and set up an edit on this empty new record in safe way?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel and insertRecord()

    Are model, the one you insert to, and _model, the one your mapper looks at, the same object? Is that just a typo?

  3. #3
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel and insertRecord()

    Quote Originally Posted by ChrisW67 View Post
    Are model, the one you insert to, and _model, the one your mapper looks at, the same object? Is that just a typo?
    It is a typo.

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

    Default Re: QSqlTableModel and insertRecord()

    What is the editStrategy() on the model?

    This code:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10.  
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    12. db.setDatabaseName(":memory:");
    13. db.open();
    14. QSqlQuery query;
    15. query.exec("CREATE TABLE test (a INTEGER, b VARCHAR(10))");
    16. query.exec("INSERT INTO test VALUES (1, 'One')");
    17. query.exec("INSERT INTO test VALUES (2, 'Two')");
    18. query.exec("INSERT INTO test VALUES (3, 'Three')");
    19.  
    20.  
    21. model.setTable("test");
    22. model.select();
    23.  
    24. // model.setEditStrategy(QSqlTableModel::OnRowChange);
    25. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    26.  
    27. qDebug() << "Starting with" << model.rowCount();
    28. for (int i = 0; i < 20; ++i) {
    29. QSqlRecord record = model.record();
    30. qDebug() << i << model.rowCount();
    31. model.insertRecord(-1, record);
    32. }
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    Works as expected with one value and not the other.

  5. #5
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel and insertRecord()

    Thanks for your reply. I think that I now know how to work with Model / View Framework.

Similar Threads

  1. Replies: 8
    Last Post: 30th March 2011, 20:06
  2. QSqlTableModel and LIKE
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:45
  3. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 7th March 2006, 19:40
  4. QSqlTableModel
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 12:35
  5. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 26th January 2006, 07: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.