To make it simple, my application has two scroll bars. when any scroll bar changes, the QPainter re-paints images.
code pieces:
// connect valueChanged to update
connect(this->horizontalScrollBar(),
SIGNAL(valueChanged(int)),
this,
SLOT(UpdateVisibleArea()));
connect(this->verticalScrollBar(),
SIGNAL(valueChanged(int)),
this,
SLOT(UpdateVisibleArea()));
in UpdateVisibleArea(), the QWidget.update() method is called. and the QWidget.paintEvent() method is implemented like the following:
QPixmap q_pixmap;
if (!ImageCache::GetPixmap(cache_key, &q_pixmap)) {
// there're up to thousands of images, we can not pre-load them all into the cache.
// the following statement may take 80~250ms.
loadImage(cache_key, &q_pixmap);
ImageCache::PutPixmap(cache_key, q_pixmap);
}
QPainter q_painter;
q_painter.drawPixmap(...); // usually it takes less than 20ms.
The problem:
when I drag scroll bars quickly, it feels pretty laggy.
My thoughts:
- The images should be loaded in background thread. but the problem is, the qpainter instance is waiting on the UI thread. it can not proceed when the image is not ready.
- Is there any way to prevent repainting happening so fast when scroll bar changes quickly?
My question:
Can you please give me some clue on how to optimize it?