PDA

View Full Version : How to disable "lasy population" in QSqlRelationalTableModel?



Husky
18th October 2012, 17:00
Hello!
I'm creating a model, connected with a db-table from SQlite and show it using tableView. Here is my code:

QSqlRelationalTableModel *model=new QSqlRelationalTableModel;
model->setTable("publications");
model->setRelation(6, QSqlRelation("pub_types", "id", "type"));
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
while(model->canFetchMore())
model->fetchMore();
ui->tableView_pub->setModel(model);
ui->tableView_pub->setFocus();
ui->tableView_pub->selectRow(model->rowCount()-1);
QSqlRelationalTableModel class is implemented with a so-called "lazy population". When i'm working with table, it loads only first 255 rows from database, and the rest only after scrolling. I don't need it, I want to load all data at once (there is enough memory and database is not too large)
Everywhere I can, I use this code: "while(model->canFetchMore()) model->fetchMore();". It works just fine, tableview always loads all data at once.
However, I also need to use OnFieldChange editing strategy. Herewith, when user finishes editing of a cell from a row with number more than 255, table in database changes, and tableview load data again. But, this time I can't insert my "while(model->canFetchMore()) model->fetchMore();" anywhere, because it is hidden somewhere inside the class. As a result, after editing tableView jumps to 255-th row. This is very inconvinient.
My question is, how to change this situation? How to change methods of QSqlRelationalTableModel to load all data at once, or somehow increase this 255-rows limit?

Ashkan_s
18th October 2012, 20:09
In this case you can implement a slot which loads all the data and connect the QAbstractItemModel::dataChanged signal of the model to it.

Added after 22 minutes:

You may also try to subclass the QSqlRelationalTableModel and re-implement its QSqlRelationalTableModel::select method and fetch all the data here or emit some signal you have defined and then connect the signal to a slot which loads all the data. seems working.

Husky
19th October 2012, 09:39
Thanks a lot!
I subclassed my own class QSqlRelationalTableModelExtraFetch from QSqlRelationalTableModel and reimplemented select() this way:


bool QSqlRelationalTableModelExtraFetch::select()
{
bool result;
result = QSqlTableModel::select();
while(this->canFetchMore())
this->fetchMore();
return result;
}

Just what I need.