I wanted to use openGL
to render QGraphicsview
items so that I can save CPU power. In the documentation it states that what I have to do is add a QOpenGLWidget
as the view port of the QGraphicsview
. This is what I'm doing:
QOpenGLWidget *glWidget = new QOpenGLWidget();
ui->drawScreen->setViewport(glWidget);
ui->drawScreen->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
ui->drawScreen->update();
(ui->drawScreen
is my QgraphicsView
)
When I draw something on the screen it draws correctly but after a certain time I want to remove the item with a fade animation. That isn't provoking the screen to update, i.e., when I do this:
double opacity = 1.0 - ((elapsed - annotationFrameOut) / (double)fadeOutFrames);
(*i)->setOpacity(opacity);
(*i)
is one item.
Or this:
scene.removeItem(item);
Visually, nothing happens. Only when I re-size the screen does the item disappear, or show the correct opacity in case it's fading.
Also, when I move an item no position disappears so it creates a dragging effect. Same for the selection rectangle or when typing in a text item (the bounding rect is drawn multiple times).
This seems like a simple update problem but I can't figure out what to do, nor do I seem to find this issue online. I am using Windows.
Thank you for your time.
EDIT:
Picture exemplifying the problem. Blue rectangles are the selection rectangles that are never removed (unless I re-size the window)
EDIT 2:
Code using QGLWidget
QGLWidget *viewPort = new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::AlphaChannel), ui->drawScreen);
ui->drawScreen->setViewport(viewPort);
ui->drawScreen->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
ui->drawScreen->update();
EDIT 3: In my app I have a video playing bellow the QGraphicsView. I show the video by updating a QLabel every time a new frame comes up.
Instead of using a QLabel
I tried to update the QPixmap
of a QGraphicsPixmapImage
. If now I draw on top of this image the drawing is being correctly refreshed, I guess because the picture is being updated constantly. But outside of the picture it still doesn't work.
QGraphicsView
class. Inside of it, I have a method that checks when the drawn items should be erased (based on frames from a video). In there I have tried to doviewport()->update()
or call the view'supdate()
but it isn't working. WithoutopenGL
this works well (even without that call). Do I have to extend theQOpenGLWidget
and implement my ownpaintGL
? It seems that not only my operations, but anything that means removing a drawn item does not update (e.g. selection rectangle). – Solidusupdate()
is wrong. The whole point of usingQGraphicsView
is that such things are handled automatically. Your comment about "mixing" two rendering APIs is wrong too - Qt fully supports usingQPainter
on an OpenGL context. This is supposed to work, and indeed it did work with the oldQGLWidget
. – Kuba hasn't forgotten Monica