Results 1 to 7 of 7

Thread: drawForeground update trouble

  1. #1
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default drawForeground update trouble

    I'm having problems using the QGraphicsView to implement overlays. I simply would like to draw a semi-transparent box with fixed widget coordinates, containing some text. I thought drawForeground() would be the perfect solution. I get the widget coordinates from the viewport(ignoring the scrollbars) turn of the world matrix and paint.

    The result looks good at first, but then something goes wrong. The viewport seems to cache the contents and doesn't always ask for an update when I scroll.
    I've attached to screenshots and a full example. Does anybody have an idea how I can provoke an update, or if I am doing something wrong? I'm using Qt 4.3.2 on a windows machine
    Qt Code:
    1. #include <QtGui>
    2.  
    3. QString text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
    4. "sed do eiusmod tempor incididunt ut labore et dolore magna "
    5. "aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
    6. "ullamco laboris nisi ut aliquip ex ea commodo consequat. "
    7. "Duis aute irure dolor in reprehenderit in voluptate velit ...";
    8.  
    9. class GraphicsView : public QGraphicsView
    10. {
    11. public:
    12. GraphicsView(QGraphicsScene* scene)
    13. : QGraphicsView(scene)
    14. {
    15. }
    16. QRectF overlayRect()const
    17. {
    18. return QRectF(viewport()->rect().width()-220, 20, 200, 200);
    19. }
    20. void drawForeground(QPainter *painter, const QRectF &rect)
    21. {
    22. painter->save();
    23. painter->setWorldMatrixEnabled(false);
    24. painter->setPen(QPen(Qt::darkGray, 2));
    25. painter->setRenderHint(QPainter::Antialiasing);
    26. painter->setBrush(QColor(0,0,0,10));
    27. painter->drawRoundRect(overlayRect());
    28. painter->drawText(overlayRect().adjusted(20, 20, -20, -20), Qt::TextWordWrap , text);
    29.  
    30. painter->setWorldMatrixEnabled(true);
    31. painter->restore();
    32. QGraphicsView::drawForeground(painter, rect);
    33. }
    34. void scrollContentsBy(int dx, int dy)
    35. {
    36. // can I force an update here
    37. QGraphicsView::scrollContentsBy( dx, dy);
    38. }
    39. };
    40.  
    41. int main(int argc, char* argv[])
    42. {
    43. QApplication app(argc, argv);
    44. scene.setSceneRect(0,0,2000,2000);
    45. GraphicsView view(&scene);
    46. view.show();
    47. return app.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

    P.S. The problem goes away if I pass the flag QGraphicsView::FullViewportUpdate, or use an OpenGL viewport, but I need a software rendering mode and would rather keep the updates to a minimum.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: drawForeground update trouble

    Seems what you're wanting is a viewport aligned overlay item. Graphics View doesn't support this, and drawForeground() is the wrong function to reimplement - it's there to render the front layer of the scene, in scene coordinates. Disabling the matrix is a hack that rarely gets you where you want.

    Usually people implement overlays for QGraphicsView by reimplementing paintEvent(), calling the base implementation and simply rendering on top of the viewport contents with QPainter. To avoid the problems with scrolling, you can either reimplement the scrollContentsBy() function and update the viewport with the overlay region, or set the viewportUpdateMode() to FullViewportUpdate. Using an OpenGL viewport also solves the scrolling problem without any extra action needed.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  3. #3
    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: drawForeground update trouble

    Maybe moving the overlay item in the scene as the viewport gets scrolled might be an option as well? I know it's stupid and you shouldn't do that, but it might just work. Of course it'll break once you start zooming...

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: drawForeground update trouble

    Thanks guys, I guess I was so thrilled about GraphicsView I tried to use it as a Golden Hammer. Perhaps this functionality should be provided by the graphics view/scene framework, though. All I need is a function to invalidate a certain area in the viewport. I always thought the scene was all about scene coordinates and the view all about the screen display. I'm of good hope, since last time I found a feature lacking within the framework you came up with QGraphicsItem::ItemIgnoresTransformations in no time.

  5. #5
    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: drawForeground update trouble

    Quote Originally Posted by spud View Post
    All I need is a function to invalidate a certain area in the viewport.
    There is QGraphicsScene::invalidate and QWidget::update(QRect).

  6. #6
    Join Date
    Jun 2009
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: drawForeground update trouble

    Thanks for your code!

    Actually you can add
    this->viewport()->update();
    to solve your problem.

  7. The following user says thank you to acestrong for this useful post:

    crayzeewulf (28th March 2011)

  8. #7
    Join Date
    Nov 2008
    Location
    San Diego, CA
    Posts
    1
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: drawForeground update trouble

    Thanks acestrong. I had the same problem and the viewport update trick you suggested worked like a charm.

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.