Results 1 to 10 of 10

Thread: QAbstractScrollArea forces repainting the whole viewport

  1. #1
    Join Date
    Dec 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QAbstractScrollArea forces repainting the whole viewport

    Hi!

    I'm developing a personal project in which I use a QAbstractScrollArea. Its viewport is the parent of another widget (subclass of QWidget) in which the drawing is made. While I was analysing the drawing performance on the viewport, I realized that, whenever I scroll the widget (using move()), the widget gets a paintEvent whose boundingRect is the size of the whole viewport.

    The problem with this behaviour is that it forces the widget to redraw the entire visible area every time it is scrolled. If I use a QScrollArea instead (which I can't use because I need a finer-grained control over the viewport), when you scroll the widget moving the bars then the paintEvent which the widget receives has a region() the exact size of the scrolled area, and then the repainting is optimal.

    I don't understand what I'm doing wrong, if anyone could help...

    Thank you!

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

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    It makes perfect sense to redraw the whole viewport when it is scrolled for an abstract scroll area. The base class doesn't know how much of the viewport actually changes when scroll is requested so it has to update everything unless a subclass says otherwise (probably from within QAbstractScrollArea::scrollContentsBy() - even the docs say so).
    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.


  3. #3
    Join Date
    Dec 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    Hi,

    Thanks for your fast answer! And what should I do in order to repaint only the needed areas? If I call viewport()->repaint(the_proper_area) in scrollContentsBy(), the widget is still asked for a viewport-size repaint when being moved from scrollContentsBy

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

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    First of all use update() instead of repaint(). Second of all if you move a widget around then how much of it would you like to repaint if the background under it may be changing? I don't know what you are doing or what you are trying to do so it is hard to help you. Foremost I don't understand why you can't use QScrollArea instead of its base class.
    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.


  5. #5
    Join Date
    Dec 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    What I am trying to do is implementing a plain text editor. I need to use a QAbstractScrollArea instead of a QScrollArea because I want it to scroll lines instead of pixels when receiving a wheel event. Moreover, I'd like to have in the viewport another widget which shows line numbers and, when receiving horizontal scroll events, scroll only the widget with the text but not the widget with the line numbers.

    I have already that working, but every time I move the widgets I receive paint event the whole size of the viewport. I can't use a QScrollArea because I can't scroll horizontally only part of the widgets shown as I need.

    How can I tell the QAbstractScrollArea or the viewport() to update() only the needed parts?

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

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    Quote Originally Posted by Charletes View Post
    What I am trying to do is implementing a plain text editor.
    And what is wrong with QPlainTextEdit?

    I need to use a QAbstractScrollArea instead of a QScrollArea because I want it to scroll lines instead of pixels when receiving a wheel event.
    I don't see how QScrollArea is preventing you from doing that.

    Moreover, I'd like to have in the viewport another widget which shows line numbers and, when receiving horizontal scroll events, scroll only the widget with the text but not the widget with the line numbers.
    That's not how it's done. Use QAbstractScrollArea::setViewportMargins() and place your line counting widget in the blank space left by the viewport.
    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.


  7. #7
    Join Date
    Dec 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    Quote Originally Posted by wysota View Post
    And what is wrong with QPlainTextEdit?
    I'd like to be able to implement a column-mode, and as far as I know that can't be done using QPlainTextEditor.

    That's not how it's done. Use QAbstractScrollArea::setViewportMargins() and place your line counting widget in the blank space left by the viewport.
    I didn't know that it could be done that way. I'll try it, it seems to be easier that way.

    Thanks!

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

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    Quote Originally Posted by Charletes View Post
    I'd like to be able to implement a column-mode, and as far as I know that can't be done using QPlainTextEditor.
    I don't know what "column mode" is but there is a good chance it can be implemented on top of what QPlaintTextEdit offers.
    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.


  9. #9
    Join Date
    Dec 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    What most text editors call column-mode is the posiblity on writing on several lines at once and being able to select a square of text instead of lines. Emacs call it rectangle mode I think.

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

    Default Re: QAbstractScrollArea forces repainting the whole viewport

    I don't see a reason why this couldn't be implemented with QPlainTextEdit.
    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.


Similar Threads

  1. QAbstractScrollArea metrics
    By Olivier Berten in forum Qt Programming
    Replies: 0
    Last Post: 1st February 2010, 08:18
  2. Non-modal dialog forces app to exit.
    By vereteran in forum Newbie
    Replies: 1
    Last Post: 17th October 2009, 08:39
  3. QScrollArea vs. QAbstractScrollArea
    By zaphod.b in forum Qt Programming
    Replies: 15
    Last Post: 7th July 2009, 15:26
  4. QAbstractScrollArea::setViewportMargins() not working :(
    By montylee in forum Qt Programming
    Replies: 7
    Last Post: 21st January 2009, 15:52
  5. QAbstractScrollArea and paintEvent
    By babu198649 in forum Newbie
    Replies: 13
    Last Post: 4th August 2008, 10:56

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.