3

For some (well, performance) reason, Qt's "model" classes only fetch 256 rows from the database so if you want to append the row to the end of the recordset, you, apparently, must do something along the lines of

while (model->canFetchMore()) {
  model->fetchMore();
}

This does work and when you do model->insertRow(model->rowCount()) afterwards, the row is, indeed, appended after the last row of the recorset.

There are various other problems related to this behaviour, such as when you insert or remove rows from the model, the view that renders it gets redrawn with only 256 rows showing and you must manually make sure, that the missing rows are fetched again.

Is there a way to bypass this behaviour altogether? My model is very unlikely to display more, than, say, 1000 rows, but getting it to retrieve those 1000 rows seems to be a royal pain. I understand, that this is a great performance optimization if you have to deal with larger recordsets, but for me it is a burden rather than a boon.

The model needs to be writable so I can't simply use QSqlQueryModel instead of QSqlRelationalTableModel.

shylent
  • 10,076
  • 6
  • 38
  • 55
  • 2
    Reading the docs on `QSqlTableModel::insertRecord(int row, const QSqlRecord& record)` I see _"If row is negative, the record will be appended to the end."_ So, instead of using `model->rowCount()` you could just pass `-1`. Does that changes anything about your problem? – dschulz May 26 '12 at 01:32

1 Answers1

4

From the QSqlTableModel documentation:

bool QSqlTableModel::insertRecord ( int row, const QSqlRecord & record )

Inserts the record after row. If row is negative, the record will be appended to the end. Calls insertRows() and setRecord() internally.

Returns true if the row could be inserted, otherwise false.

See also insertRows() and removeRows().

I've not tried yet, but I think that it's not necessary to fetch the complete dataset to insert a record at the end.

dschulz
  • 4,666
  • 1
  • 31
  • 31
  • I have no idea how I've missed this bit. Thanks a lot! I will try to see if it really works as advertised. If it does, it certainly will at least help me with appending the rows to the end of the recordset. – shylent May 26 '12 at 09:08