c++qtuser-interfaceqglwidget

Using UI design files and having a custom widget with custom constructor QT


I have 2 custom widgets inheriting from QGLWidget and I aim to show the same scene from 2 different views using these widgets. For this I share the first widget with the second widget. However this requires a custom constructor than the compiled UI file provides. Such as

   // The line I mention from Compiled UI file
    widget_2 = new SideGlWidget(widget);
    // What I actually want this line to be
    widget_2 = new SideGlWidget(widget, MainScreen);

Some suggests to set such additional parameters later using an init function. Then how do I set shareWidget member of QGLWidget? here is the contructor of SideGlWidget

SideGlWidget::SideGlWidget(QWidget *parent,QGLWidget * shareWidget)
    : QGLWidget(parent,shareWidget)
{

}

Any comments and edits are welcome.


Solution

  • Here is the official and latest solution to this problem from http://doc.qt.io/qt-5/qopenglwidget.html:

    When multiple QOpenGLWidgets are added as children to the same top-level widget, their contexts will share with each other. This does not apply for QOpenGLWidget instances that belong to different windows.

    Therefore by inheriting from QOpenglWidget instead of QGlWidget you dont have to deal with context sharing at all. Beware that updategl function is replaced with update function.