c++qtopenglqopenglwidget

QOpenGLWidget in new window


I have a simple UI with a button, after pressing the button I should open a new window where OpenGL stuff should be drawn. I'm trying to do this with QOpenGLWidget but the InitializeGL() and paintGL() functions are never called.

So basically I have my main with a function that should start everything:

void test::displayOpenGLObjects
{
    newOpenGLContext = std::make_unique<Visualization_OpenGL>(this);

}

Visualization_OpenGL.cpp

test::Visualization_OpenGL(QWidget *parent): QOpenGLWidget(parent)
{  
    QOpenGLWidget *test_window = new QOpenGLWidget();
    test_window->setWindowTitle("OpenGL Visualization");
    test_window->makeCurrent();
    test_window->show();
}

test::initializeGL(){
...
}

test::paintGL(){
...
}

test::resizeGL(){
    ...
}

And the header:

class Visualization_OpenGL : public QOpenGLWidget, public QOpenGLFunctions
{
    Q_OBJECT

public:
    Visualization_OpenGL(QWidget *parent);
    ~Visualization_OpenGL();
protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

};

The examples I've seen all draw everything on the mainwindow, and when I do it like that, the paintGL() and initializeGL() are indeed called, but I need to have a separate window for this, I'm guessing my problem is something involving the QOpenGLWidget *test_window = new QOpenGLWidget(); but I'm not sure how to fix. I'm super new with this so I'm sorry if this is a super noob question, but any help will be appreciated.


Solution

  • It is strange to open one widget in the constructor of another. You can create a new widget in a button click slot. For example:

    void MainWindow::OnButtonClicked()
    {
        Visualization_OpenGL* test_window = new Visualization_OpenGL();
        test_window->setWindowTitle("OpenGL Visualization");
        test_window->makeCurrent();
        test_window->show();
    }
    

    Then Visualization_OpenGL::initializeGL, Visualization_OpenGL::resizeGL and Visualization_OpenGL::paintGL will be invoked.