PDA

View Full Version : Correctly refreshing a view after a model update.



SK
9th April 2006, 17:37
Hello,
what I want to achieve is the following:
I have data from a sql database in a subclass of QSqlQueryModel and refresh this model every 10 seconds from the database. The user ideally shouldn´t notice the refresh at all. The view is set so it selects whole rows only, and only one at a time.
So far, I save the rowID selected in the view (not the QModelIndex but a non-ambiguous value in the table), then refresh the model and afterwards reselect the saved rowID in the view.
The problem now is that this reselection in the view automatically jumps to the selection, even if the selected row was far down the scrollbar originally. Also, when the project is finished the data in the sql database will grow and new items will automatically be added with the 10 second refresh. What are your suggestions to achieve a refresh with minimal optical impact on the view and hence the user?

Thanks in advance ;)

wysota
10th April 2006, 09:56
Did you try using QSqlQueryModel::fetchMore()?

If that doesn't work, you should inherit the model and provide your own means to refresh it by selecting the data yourself, scanning the result for changes and emitting proper signals (layoutChanged() and dataChanged()) from the model for the view to update itself.

Conel
12th April 2006, 09:51
The best way (IMHO) is to inherit model from QAbstractTableModel and apply the following algorithm for this model:
1) read all desirable items from the database on the start-up and store them in your model
2) while refreshing model read ONLY newly added items and insert them to model.
You should use beginInsertRows() and endInsertRows() calls. This automatically emits signals layoutChanged()

I implemented this schema for my database (rows in this database are just log messages from some external process). Each row has unique ID which is autoincremented so I easily can select only new rows from the database

wysota
12th April 2006, 11:35
2) while refreshing model read ONLY newly added items and insert them to model.

And what if one of existing records was updated? or deleted? or you can't distinguish between added later and added earlier? In some cases what you say is possible, but you have to assume many things. Without assumptions you have to reread the whole query.

Conel
12th April 2006, 15:09
Yes, this works only with some assumptions. In my case the records could not be modified or deleted

If database may be changed in any way then the only solution is to fully reread all the table contents.

Nuria
20th April 2006, 10:07
Hi
I'm trying to update a model that takes the dates from a qprocess output. When it's reading the dates, my application is blocked for a moment and I don't know how to solve it.

Can you help me?



void MainWindow::updateTable()
{
/*Read condor_status -l*/
QModelIndex parent;
int numRow=0;
int maxRow = m_model->rowCount(parent);
QString line;

while((line = m_proc->readLine()).size() > 1){
do{
if(numRow >= maxRow){
m_model->insertRows(numRow,1,parent);
maxRow = m_model->rowCount(parent);
}
QString dataNew = line.split(' ', QString::SkipEmptyParts)[2];
QString header = line.split(' ',QString::SkipEmptyParts)[0];
int numColumn = headerData(m_model,header);
QModelIndex index = m_model->index(numRow,numColumn,parent);
QString dataOld = (m_model->data(index)).toString();

if(dataOld != dataNew){
m_model->setData(index, dataNew);

}
}while((line = m_proc->readLine()).size() > 1);
numRow++;
}

QTimer::singleShot(5000, this, SLOT(createProcess()));

}

wysota
20th April 2006, 10:23
Do it in a separate thread or use a timer which will fire once in a while and read and process the input which is available at that moment. When the process ends and its data is processed, kill the timer.