views:

440

answers:

3

In a Qt4 application, is it possible to tell inside a paintEvent() handler whether the repaint was triggered by a resize or not?

I have a widget which is very slow to redraw (a complicated plot), and I want to speed up resizes by just blitting a resized pixmap while the widget is being resized, and only redraw the widget when the resize is complete.

I've tried setting/unsetting a flag at the beginning and end of resizeEvent() but that doesn't seem to work (i.e. the flag is always off in paintEvent()).

+3  A: 

I don't think you can easily do this. It's kind of hard to tell when resizing started/stopped, especially in a cross-platform way. I'd probably have a single-shot timer triggered by resizeEvent, that would render the image onto a QPixmap. If you get another resizeEvent while the timer is still active, just restart it. In paintEvent always paint the current pixmap, and after you render a new pixmap from the timer, call update() on the widget. Not an ideal solution, but it should work.

Lukáš Lalinský
Thanks, I implemented something like this and it seems to be working well. I'm also restarting the timer if the left mouse button is down, so if you "pause" resizing it won't trigger a redraw.
dF
+2  A: 

One approach you could take is to always paint the pixmap, but remember to recreate the pixmap "soon" if the window size has changed.

So, when the paintEvent comes in, if the size is different to current pixmap size, then paint the stored pixmap anyway, but then set (or reset) a QTimer to trigger a signal to a slot which will refresh the pixmap.

When this refresh method runs, it will re-render the pixmap and request a refresh of the widget.

Paul Dixon
+1  A: 

Another possibility would be to have a look at how QMdiSubWindow::RubberBandResize is implemented internally, it may provide hints as to how to achieve this. Works only on a QMdiSubwindow if I recall correctly - it 'stops' painting (except a small window border with a transparent body) during resizing and fires one final resizeEvent (which triggers update/paint) when you stop resizing the window...

ChristopheD