简体   繁体   中英

Using QOpenGLWidget with QGraphicsView: viewport not updating?

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, ie, 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.

The explicit update() call is unnecessary. This looks like a Qt bug. The QGraphicsView does some magic to a QGLWidget to make it work as a viewport. Most likely, this magic hasn't been ported to work with QOpenGLWidget .

As a workaround, use QGLWidget instead.

It seems the problem lies with the stylesheet I used to change the background color of the view. Adding a stylesheet to the widget also produces the same bug. With a stylesheet the view-port just becomes black and the refresh of items doesn't seem to work properly.

I removed the stylesheet and changed the background-color by re-implementing the drawBackground function.

Thanks to Kuba Ober for helping me figure out this.

样式表会以某种方式生成重绘错误,即使在我的情况下,我也删除了样式表,并且一切正常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM