Multiple Paint event calls Slowing down the application



  • Hi, I'm using a slidebar's valuechanged() property to call a fairly hefty paint event. But doing this sometimes slows down the application considerably. My assumption is that the paint event is getting called faster than it is capable of executing and that they are needlessly queuing up or clashing in some way.

    I'm looking for a way to call the paint event which will cancel the next call (not wait) if the paint event is still busy from the previous one. Alternatively I figured I might have to add a timer and call the paint event periodically instead of directly from the slidebar.

    Any thoughts/advice?

    Thanks,
    Woody



  • First, are you trying to call paint directly from the valuechanged() slot?, or are you calling update() on the widget to be repainted?

    Assuming you're calling update(), and letting Qt decide when to call your paintEvent() [NEVER call paintEvent() directly] and you're still getting undesirable results, one option would be to avoid calling update() again until the paintEvent() has already run... For instance:

    @
    int myClock = 0;
    int lastPaintEventTime = 0;
    ...
    void YourWidget::paintEvent(PaintEvent *ev)
    {
    lastPaintEvent = myClock;
    ... paint away ...
    }
    ...
    void YourWidget::on_slider_valueChanged(int value)
    {
    if(lastPaintEvent >= myClock) {
    ++myClock;
    update();
    }
    }
    ...
    @

    This way, you don't call update() again until paintEvent() has run since the last time you called update(). However, I'm really surprised that the event loop wouldn't be collapsing update requests like it's supposed to.

    If it's just simply too slow because the paint event is too hefty, then you might want to use a timer event called every 500ms or whatever, and call update() from the timer event only if the slider value has changed since the last time things were painted.



  • Thank you for the reply;

    Yes, I am calling update() rather than the PaintEvent directly. I tried a similar idea tot he one you suggested without success, but i think Ive just found the real problem with the program: it has a memory leak that only became apparent when the paintevent got called frequently enough.

    I have now identified the problem in the code half way through writing this reply: when I remove all the painter.save(); commands there is no longer a memory leak :D

    so just to detail what I done in the last half an hour:
    1.)
    removed all the painter.save(); functions which dont appear to be necessary

    This gave continuous "QPainter::restore: Unbalanced save/restore" errors in Qt. (which is why I put the save() functions in to start with)
    2.)
    remove all the painter.restore(); functions (these were only there because I copied the example, as with .save() I dont truly know what they do)

    3.) enjoy a program that functions identically to before but without memory leaks and without warnings in Qt.

    For reference It would be nice to know how to stop this problem if it ever happens that you need the save/restore functions.

    Cheers,
    Woody


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.