Results 1 to 3 of 3

Thread: Painting over QOpenGLWidget in QGraphicsview clears viewport

  1. #1
    Join Date
    Apr 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Painting over QOpenGLWidget in QGraphicsview clears viewport

    I'm making an application where I draw an image on a QGraphicsScene and then draw items over the scene in viewport coordinates (as I always want the items to appear in the same place, regardless of transformations of the scene).

    In order to accomplish this I created the following subclass of QImageView:

    Qt Code:
    1. class ImageView : public QGraphicsView
    2. {
    3. public:
    4. ImageView(QWidget* parent = nullptr)
    5. :QGraphicsView(parent)
    6. {
    7. }
    8.  
    9. protected:
    10. void paintEvent(QPaintEvent* event)
    11. {
    12. QGraphicsView::paintEvent(event);
    13. QPainter painter(viewport());
    14. painter.setPen(QPen(Qt::green));
    15. painter.drawRect(0, 0, 100, 100);
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 

    When using the standard viewport, this code works perfectly. However, when I set a QOpenQLWidget as viewport (using "myView->setViewport(new QOpenGLWidget());"), it will no longer paint
    the image in the scene, only the green rectangle from the paintEvent. If I comment out the last 3 lines of the paintEvent, the image is drawn normally.

    Through a bit of experimentation I learned that creating a QPainter with the QOpenGLWidget, will clear the widget.

    As an alternative option I tried creating a class derived from QOpenGLWidget, overriding the paintGL() method (so that it paints the green rectangle) and then setting it as the viewport in a standard QGraphicsView. This didn't work either as the paintGL method is never called when the widget is used as a viewport.

    So my questions are simple: Why doesn't the above code work with QOpenGLWidgets and how can I make it work?

    To sum it up I need to do the following things:
    • Draw an image on a QGraphicsScene (or anywhere elsewhere I can zoom and translate at will).
    • Draw items over the image at a fixed place (so that they are always visible and not affected by zoom or translation).
    • It is important to note that the things I draw may not be known at compile time (in the actual application I draw a QPicture, which can contain anything).


    The code example above is the smallest class I could create where I could recreate the problem, if you need more information please let me know.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,067
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Painting over QOpenGLWidget in QGraphicsview clears viewport

    Draw items over the image at a fixed place (so that they are always visible and not affected by zoom or translation).
    Have a look at QGraphicsView::drawForeground(). I use a class derived from QGraphicsView where I actually have two scenes. One of them is the conventional scene that is drawn in the paint event, the other is a special scene that holds the overlay objects. This one is drawn in drawForeground() and can have a transformation matrix different from the other scene. (The widget is actually more complex than that - it also uses the drawBackground() method to draw a bitmap image which is then overlaid by the scene, which is then overlaid by the foreground scene).

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

    KaBro (28th April 2016)

  4. #3
    Join Date
    Apr 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Painting over QOpenGLWidget in QGraphicsview clears viewport

    Thank you for your reply,

    I replaced the paintEvent method in my class (see start of thread) with the following method:
    Qt Code:
    1. void drawForeground(QPainter* painter, const QRectF& rect)
    2. {
    3. QGraphicsView::drawForeground(painter, rect);
    4.  
    5. painter->save();
    6. painter->resetMatrix();
    7. painter->resetTransform();
    8. painter->setPen(QPen(Qt::green));
    9. painter->drawRect(QRect(0, 0, 100, 100));
    10. painter->restore();
    11. }
    To copy to clipboard, switch view to plain text mode 

    It looks like this works exactly as I need it to (no problems with zooming or translations).

Similar Threads

  1. QOpenGLWidget + QGraphicsView and performance
    By tanshihaj in forum Qt Programming
    Replies: 0
    Last Post: 25th January 2016, 09:50
  2. QGraphicsView inside of QAbstractItemView viewport
    By Paladin12 in forum Qt Programming
    Replies: 5
    Last Post: 24th January 2016, 14:15
  3. Detect changes to the Viewport in QGraphicsView
    By Casper14 in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2014, 11:21
  4. Replies: 3
    Last Post: 12th October 2012, 10:44
  5. Replies: 5
    Last Post: 9th January 2012, 21:22

Tags for this Thread

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.