Results 1 to 7 of 7

Thread: Scrolling in scaled Painter area causes lack of re-painting

  1. #1
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Scrolling in scaled Painter area causes lack of re-painting

    Hi Everyone.
    I have taken the Scribble Example from Qt 4.8 and done some modifications to it in hope to later do it to my own, similar project. For those of you not familar with the Scribble Example it lets you draw, basically like the pencil tool in windows paint.

    Anyway, have used QScrollArea to make the window scrollable which works fine when the scribbleArea is at a its original scale (1.0).
    I increased the scale, to enlarge the graphics using the QPainter::scale() function and change the scale to 1.25. When I scroll at this new scale the window gets lines like the widget is not re-painting properly until I use the pencil again at which point the lines go away and the scribbleArea looks normal again.

    scribbleBad.JPG
    Notice the lines on the top and left side of the white scribbleArea.

    Here is what I have modified to achieve this:
    the ScribbleArea:: paintEvent() function for scaling
    Qt Code:
    1. void ScribbleArea::paintEvent(QPaintEvent *event)
    2. //! [13] //! [14]
    3. {
    4. QPainter painter(this);
    5.  
    6. painter.scale(1.25, 1.25); //ADDED LINE FOR SCALING
    7.  
    8. QRect dirtyRect = event->rect();
    9. painter.drawImage(dirtyRect, image, dirtyRect);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    The ScribbleArea::drawLineTo() function to make it update the whole scribble area rather than just a small section
    Qt Code:
    1. void ScribbleArea::drawLineTo(const QPoint &endPoint)
    2. //! [17] //! [18]
    3. {
    4. QPainter painter(&image);
    5. painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
    6. Qt::RoundJoin));
    7. painter.drawLine(lastPoint, endPoint);
    8. modified = true;
    9.  
    10. //int rad = (myPenWidth / 2) + 2;
    11. //update(QRect(lastPoint, endPoint).normalized()
    12. // .adjusted(-rad, -rad, +rad, +rad));
    13. update(); //ADDED LINE
    14. lastPoint = endPoint;
    15. }
    To copy to clipboard, switch view to plain text mode 


    In the MainWindow constructor I added QScrollArea widget
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. scribbleArea = new ScribbleArea;
    4. scribbleArea->setMaximumHeight(500);
    5. scribbleArea->setMaximumWidth(500);
    6. scribbleArea->setMinimumSize(500, 500);
    7. //setCentralWidget(scribbleArea);
    8.  
    9. scrollArea = new QScrollArea;
    10. scrollArea->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
    11. scrollArea->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
    12. scrollArea->setWidget(scribbleArea);
    13.  
    14. setCentralWidget(scrollArea);
    15.  
    16. createActions();
    17. createMenus();
    18.  
    19. setWindowTitle(tr("Scribble"));
    20. resize(500, 500);
    21. }
    To copy to clipboard, switch view to plain text mode 

    All other code is unchanged from the original example. Does anyone know what could be causing this?
    Scribble example documentation here: http://developer.qt.nokia.com/doc/qt...-scribble.html

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling in scaled Painter area causes lack of re-painting

    Connect both scroll bars (scrollArea->[horizontal/vertical]Scrollbar()) valueChanged() signal to scribble area update() slot or in scribble area paint event use image.rect() instead of dirtyRect.
    In fact both approaches do the same - they repaint whole image instead of only dirty rectangle.
    This tells you that dirty rectangle is wrong.

  3. The following user says thank you to Spitfire for this useful post:

    sdzell18 (31st January 2012)

  4. #3
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scrolling in scaled Painter area causes lack of re-painting

    Thank you, Thank you, Thank you! Using image.rect() for the dirtyRect worked perfectly.

  5. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling in scaled Painter area causes lack of re-painting

    Remember that always using whole image is not as efficient as drawing only required part of it.

  6. #5
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Scrolling in scaled Painter area causes lack of re-painting

    Yes I will keep that in mind

  7. #6
    Join Date
    Feb 2012
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Scrolling in scaled Painter area causes lack of re-painting

    Hi Spitfire and sdzell18,

    I am working on Qt 4.8 running on Linux (Ubuntu).
    Incidentally I have been doing this stuff to scroll the image using scrollBars.

    But recently I tried to implement Kinetic scrolling on the image where we paint, instead of doing all the physics from the scratch, using Flick Charm, QsKineticScroll

    As you may know, the mouseEvents on Scribble has to be sent to the ScrollArea when some mouseMove with dx of +15 && -15 pixels on the vertical direction is made while scrolling. I did ignore() in the Scribble's mouseMove and mouseRelease if the condition (dx allowance) was satisfied, but was not able to get the image to kinetically scroll.

    Can you give some idea/hint how this can be implemented? If the FlickCharm or QsKineticScroll isnt necessary, can we do this by inheriting QAbstractScrollArea properties?

    Thanks!

  8. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scrolling in scaled Painter area causes lack of re-painting

    First - I would suggest starting new thread rather than piggybacking on remotley related topic.
    Maybe some moderator can split it?

    Second - You can achieve what you want probably in many ways. As you said that you don't want to do everything from a scratch then Flick Charm or any other ready-made solution is the way to go.

    After quick look at the documentation of QsKineticScroll one thing looks like a starting point:
    When installing the event filter, the important thing to notice is that it has to be installed for both the scroll area and its viewport. A scroll area is not a single item, and failing to install the filter on the viewport will result in not getting any mouse events at all.
    As I haven't used any of those so I can't tell why it doesn't work, it's even more difficult without any code from your side.

    Please, take the extra mile and provide compilable example of your problem. Much more likely you'll get solution for your problem.

Similar Threads

  1. sometime lack string (QtEmbedded Qt4.5, linux)
    By muny in forum Qt Programming
    Replies: 2
    Last Post: 22nd November 2009, 16:51
  2. Lack of icons and standard C++ libraries
    By Trok in forum Installation and Deployment
    Replies: 0
    Last Post: 18th June 2009, 22:54
  3. excluding an area from painting using clip path
    By berliner in forum Qt Programming
    Replies: 4
    Last Post: 15th May 2009, 02:30
  4. Replies: 19
    Last Post: 26th November 2008, 19:54
  5. QRubberBand painting in the scroll area widget
    By SkripT in forum Qt Programming
    Replies: 7
    Last Post: 17th January 2006, 16:48

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.