Results 1 to 7 of 7

Thread: drawForeground update trouble

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.