PDA

View Full Version : Updating a view on demand



schall_l
14th August 2009, 09:34
I have a view attached to a model. The view gets refreshed when the model changes which is fine.

Now I would like to know if the refreshing of the view can be made on demand only.
For example, what I would like to do is to refresh the view every 500ms only, even if the model is changing faster then this.

Thanks

wysota
14th August 2009, 09:42
You can disconnect signals from the model in the view and call update() on the view periodically although it won't work very well if rows are added or removed in the model. It's better to provide a proxy model for the original model that will emit signals only at desired intervals of time - you just need to cache the changes from the original model in the proxy so that you can emit the proper signals.

schall_l
14th August 2009, 09:46
Thank you Wisota,
I found also this way to do this:


class MyView : public QTableView
{
Q_OBJECT
...
protected:
void timerEvent(QTimerEvent *event);
private:
int m_timerId;
};


MyView::MyView ( QWidget* parent ) :
QTableView(parent)
{
...
setUpdatesEnabled(false);
m_timerId = startTimer(500);
}

void
MyView::timerEvent(QTimerEvent *event) {
if (event->timerId() == m_timerId) {
setUpdatesEnabled(true);
update();
setUpdatesEnabled(false);
} else {
QWidget::timerEvent(event);
}
}

Is there anything that speaks about using setUpdatesEnabled ?

wysota
14th August 2009, 10:34
It's fine for your usecase, I guess. The only problem you may run into is that the widget will not refresh at all and you may get artifacts when i.e. dragging a window over your widget. Using a proxy would be a more reliable solution (although more difficult to implement).