If you do not specify an ORDER BY clause in your selection SQL then the order that rows are returned from an RDBMS is not defined or guaranteed. Often the retrieval order is governed by the index or indexes used during query execution. For example, a hash-based index will give what appears a random ordering. Other reasons for seemingly random order are that new rows may be inserted into database pages in the space vacated by deleted rows.
If you have an ID that is monotonically increasing then just ORDER BY that column (by using QSqlTableModel::setSort() in this case).
Bookmarks