How to update OpenGL drawings(QOpenGLWidget) when added to QGraphicsScene?

427 views Asked by At

I based my implementation on this sample. https://doc.qt.io/qt-5/qtopengl-2dpainting-example.html

What the sample does is render/animate 2 images. One is rendered using native qt functionality and the other is rendered using OpenGL. Thats all there is to it. The image that is being drawn is exactly the same.

The sample works fine. I can see both images animate. Then when I try to make changes by adding a Window class (which contains the QOpenGLWidget) inside QGraphicsScene; QOpenGLWidget stops updating itself.

Original main.cpp

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QSurfaceFormat fmt;
    fmt.setSamples(4);
    QSurfaceFormat::setDefaultFormat(fmt);

    Window window;
    window.show();

    return app.exec();
}

Changed main.cpp

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QSurfaceFormat fmt;
    fmt.setSamples(4);
    QSurfaceFormat::setDefaultFormat(fmt);

    Window window;
    //window.show();

    QGraphicsScene scene;
    scene.addWidget(&window);
    scene.addText("Hello");

    QGraphicsView view(&scene);
    view.show();

    return app.exec();
}

Here is the output.

enter image description here

Full source code is available here. https://github.com/syaifulnizamyahya/QT2dpainting

1

There are 1 answers

0
Eddy Alleman On

In the docs of QGraphicsProxyWidget *QGraphicsScene::addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags())

Note that widgets with the Qt::WA_PaintOnScreen widget attribute set and widgets that wrap an external application or controller are not supported. Examples are QGLWidget and QAxWidget.

I think you're out of luck. Could be the docs were never adapted when QOpenGLWidget was born.

Also there are some bugreports on Jira, that look very similar to your case: https://bugreports.qt.io/browse/QTBUG-44063

If I were you, to be sure I would ask on the interest mailinglists, that's where the Qt devs come regularly.