Results 1 to 12 of 12

Thread: How can I avoid the paintEvent after resizing a widget?

  1. #1
    Join Date
    Sep 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How can I avoid the paintEvent after resizing a widget?

    I am writing a custom widget using Qt 4.5 and I am looking for
    some way to optimze its refresh.
    by default Qt will invalidate all the area occupied by the widget
    after it has been resized, but this is not what I want. And I know
    there is a attribute called Qt::WA_StaticContents to let the widget
    just receive paint events only for parts of itself that are newly
    visible, but this dosen't applicable to my widget. my widget must
    refresh all the area after a vertical resizing but only need to
    refresh the newly visible part after a horizontal resizing. So I can
    use this attribute, I have to handle this in sizeEvent manually. but
    How can I avoid the paintEvent triggered by Qt after resizing a
    widget?
    Thanks

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Ignore it if you don't like flag solutions.

    Simply set a flag in resize event.
    Check that flag in paint event, If the flag is set then ignore/accept/propagate the paintevent.
    Only you will be responsible for the aftereffects of innoring the repainting after resize event.

  3. #3
    Join Date
    Sep 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Thanks for your reply. I think the solutions from you will work.
    But I have another solution: I set the attribute Qt::WA_StaticContents to my widget and check the new size and old size of the widget in resize event, if it has been vertically resized I will update(just update, not repaint) the whole area of my widget in resize event handler, but if it hasn't I'll do nothing in resize event handler. Do you think this is a feasible solution?

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Go for it.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Quote Originally Posted by jianliang79 View Post
    if it has been vertically resized I will update(just update, not repaint) the whole area of my widget in resize event handler
    What's the difference between "just update" and "repaint".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Quote Originally Posted by wysota View Post
    What's the difference between "just update" and "repaint".
    update means I just invalidate a region of the widget by calling QWidget::update(), I will receive a repaint event in the next iteration of the event loop, and multiple update will likely be merged in a single update and we only receive one repaint event for these updates.
    repaint means calling QWidget::repaint() which will call paintEvent() immediately to do the render job.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Ok, but how does it differ in your case? Qt calls update() upon resize as well...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Quote Originally Posted by wysota View Post
    Ok, but how does it differ in your case? Qt calls update() upon resize as well...
    yes, It differ in my case at least when I create my widget as a top level widget. look at the following code(from qapplication_win.cpp) :
    Qt Code:
    1. QResizeEvent e(newSize, oldSize);
    2. QApplication::sendSpontaneousEvent(this, &e);
    3. if (d_func()->paintOnScreen()) {
    4. QRegion updateRegion(rect());
    5. if (testAttribute(Qt::WA_StaticContents))
    6. updateRegion -= QRect(0, 0, oldSize.width(), oldSize.height());
    7. d_func()->syncBackingStore(updateRegion);
    8. } else {
    9. d_func()->syncBackingStore();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt will just send resizeEvent to my widget and I don't see any of the event handler will call update(). In fact it is done in d_func()->syncBackingStore(); this function will sync the backing store of the top level widget, in my case, it will draw the newly exposed area of the widget immediately(in syncBackingStore() Qt will always deem the newly exposed area as dirty even I have called repaint() in my resizeEvent handler). So if I call repaint() in my resizeEvent handler, the newly exposed area will be redrawn twice.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I avoid the paintEvent after resizing a widget?

    But why do you want to call repaint() or update() in your resizeEvent at all? It is obvious that a paint event will follow it if necessary. Maybe if you could express what is the purpose of all those manipulations we could suggest a cleaner approach?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Quote Originally Posted by wysota View Post
    But why do you want to call repaint() or update() in your resizeEvent at all? It is obvious that a paint event will follow it if necessary. Maybe if you could express what is the purpose of all those manipulations we could suggest a cleaner approach?
    Sorry, maybe I don't express my question clearly enough, now I will describe it again:
    As I have mentioned in my first post: I am writing a widget, this widget must refresh all the area after a vertical resizing but only need to refresh the newly visible part after a horizontal resizing( a ruler widget may have the same behaviour).
    1) If I don't set Qt::WA_StaticContents attribute to my widget, Qt will notify me to redraw the entire area of the widget after any kind of resizing. But in the case of horizontal resizing it will waste a lot time to redraw the unchanged part of my widget. I think this is not an effient way.
    2) If I set Qt::WA_StaticContents attribute to my widget, Qt will only notify me to redraw the newly exposed part of my widget after resizing, but in the case of vertical resizing this will make me unable to redraw the entire widget.
    It seems that I have to manually control which part of my widget need to be redrawn in resize event handler by comparing the old size and new size of my widget. I am interested in that is there an elegent way to let me control that by myself?
    Thanks.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I avoid the paintEvent after resizing a widget?

    It might prove much easier to cache the looks of your widget and simply rerender the already computed part instead of trying to work around architectures.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Sep 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I avoid the paintEvent after resizing a widget?

    Quote Originally Posted by wysota View Post
    It might prove much easier to cache the looks of your widget and simply rerender the already computed part instead of trying to work around architectures.
    Thanks. I will keep that in mind.

Similar Threads

  1. resizing widgets depending on a main widget size
    By luf in forum Qt Programming
    Replies: 6
    Last Post: 10th October 2009, 16:13
  2. Resize widget force layout resizing
    By ^NyAw^ in forum Qt Programming
    Replies: 17
    Last Post: 11th February 2009, 11:27
  3. Possible to use a widget in another widget's paintEvent?
    By robot_love in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2008, 05:18
  4. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 10:52
  5. painting a widget outside a paintEvent
    By jayw710 in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 23:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.