Solved Call PaintEvent in for loop
-
Hi guys,
I want to make a scroll test performance. It have to work like that you scroll down a document by 1px then you call paintEvent and this have to happened 1000 times. So in for loop I write:
for(int i=0;i<1000;++i){ document->m_preview->Scroll(0,1); // perform 1pixel scroll }
Problem is that the paintEvent is called only on the last scroll call. How can I force to repaint after all calling of scroll function?
update(); repaint() doesn't seems to work!
THX
-
@Tony123
What Qt class is yourdocument->m_preview
? -
@Tony123 Well, you need to learn one thing: loops block Qt event loop (applies to all event driven applications) as long as they are executed. So, what you see is what you should expect. You should use a timer (QTimer) instead of a blocking loop.
-
Try calling QCoreApplication::processEvents() after each 1px scroll
-
@Asperamanca Please not! This is bad design. OK, for testing I would agree :-)
-
@jsulm
OP states that this is just for a test. Of course such constructs should be avoided in production code. -
hehe
If you are feeling guilty ;) , you can always show how to do the
same thing with QTimer and a lambda.
But for pure testing, i agree. no harm using QCoreApplication::processEvents() -
If your goal is to measure how many time it takes to paint a widget 1000 times, so QWidget::repaint() is the right choice, otherwise, I would recommend to use QWidget::update() instead of QWidget::repaint(). In this case, using loop and QCoreApplication::processEvents(), I think it will be the same. But with another design, this will let the event-loop repaint the widget only when it's necessary.
-
@mrjj said in Call PaintEvent in for loop:
hehe
If you are feeling guilty , you can always show how to do the
same thing with QTimer and a lambda.
But for pure testing, i agree. no harm using QCoreApplication::processEvents()Not feeling guilty, because for a performance test, combining QTimer and a lambda would make it much harder to measure the actual time spent on repainting after scroll, assuming I understood the OP's intentions here correctly.
QElapsedTimer measureTimer; measureTimer.start(); for(int i=0;i<1000;++i){ document->m_preview->Scroll(0,1); // perform 1pixel scroll QCoreApplication::processEvents(); } qDebug() << measureTimer.elapsed();
and you are done, while still being very close to what the production code would do.
-
@Asperamanca thank you ..yes exactly this is what I did this helps for purpose of testing of course ty