Results 1 to 7 of 7

Thread: QSqlTableModel::insertRows, using the row parameter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QSqlTableModel::insertRows, using the row parameter

    Hello

    I have a QTableView with a QSqlTableModel as its model.

    The problem is that the newly added rows will be displayed at the end of the view, no matter what is the value of row.

    Documentation of QSqlTableModel::insertRows says: "Inserts count empty rows at position row." and in QAbstractItemModel::insertRows it says: "inserts count rows into the model before the given row."

    If "into the model" means it has no predicted effect on the view, what's the use of row?

  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::insertRows, using the row parameter

    The function does exactly what it says it does. The view may be sorting the rows, you may be using an invalid row number etc. We don't know unless you can provide a small example that demonstrates the problem. This, for example, works fine:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5. void createConnection()
    6. {
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8. db.setDatabaseName(":memory:");
    9. if (db.open()) {
    10. QSqlQuery query;
    11. query.exec("create table person (id int, "
    12. "firstname varchar(20), lastname varchar(20), primary key(firstname, lastname))");
    13. query.exec("insert into person values(101, 'Danny', 'Young')");
    14. query.exec("insert into person values(102, 'Christine', 'Holand')");
    15. query.exec("insert into person values(103, 'Lars', 'Gordon')");
    16. query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
    17. query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
    18. }
    19. }
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23. QApplication app(argc, argv);
    24. createConnection();
    25.  
    26. model.setTable("person");
    27. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    28. model.select();
    29.  
    30. QTableView view;
    31. view.setModel(&model);
    32. view.resize(640, 480);
    33. view.show();
    34.  
    35. model.insertRow(2);
    36. model.insertRow(5);
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    If you use the other edit strategies then you can only insert one row at a time (see QSqlTableModel::insertRows()) .

Similar Threads

  1. QSortFilterProxyModel::insertRows always adds rows to the end
    By Agnostic Pope in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2012, 23:23
  2. Rowcount and insertrows differences
    By tonnot in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2011, 08:56
  3. Replies: 0
    Last Post: 13th April 2011, 08:47
  4. Replies: 8
    Last Post: 30th March 2011, 20:06
  5. QTable insertRows issue
    By PrimeCP in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 09:08

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.