I'm currently trying to learn how to use QTWidgets (version 6.4.2) in C++ in VS2022. Right now, I'm trying to just run a simple ui I created in the QT designer. On the window, I have a QOpenGLWidget that I want to use to display graphics. I have this widget promoted as a MyOpenGLWidget. My issue is that when I call "initializeOpenGLFunctions()" in "initializeGL()" the code breaks with the following error:
ASSERT: "context" in file qopenglfunctions.cpp, line 155
I've tried looking through the QOpenGLFunctions page on the qt website and couldn't find any information related to this.
Here is my code:
MyOpenGLWidget.h
#pragma once
#include <QOpenGLWidget>
#include <qopenglfunctions>
class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit MyOpenGLWidget(QWidget* parent = nullptr) : QOpenGLWidget(parent)
{
initializeGL();
}
~MyOpenGLWidget()
{
}
void Initialize()
{
initializeGL();
}
protected:
void initializeGL()
{
initializeOpenGLFunctions(); // Code breaks when this is called
}
void paintGL() {}
void resizeGL(int w, int h) {}
};
QT_OpenGL.h
class QT_OpenGL : public QMainWindow
{
Q_OBJECT
public:
QT_OpenGL(QWidget *parent = nullptr);
~QT_OpenGL();
private:
Ui::QT_OpenGLClass * ui;
protected:
};
QT_OpenGL.cpp
#include "QT_OpenGL.h"
QT_OpenGL::QT_OpenGL(QWidget *parent)
: QMainWindow(parent)
{
ui = new Ui::QT_OpenGLClass();
ui->setupUi(this);
}
QT_OpenGL::~QT_OpenGL()
{}
main.cpp
#include "QT_OpenGL.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QT_OpenGL w;
w.show();
return a.exec();
}
Thanks to @holtavolt 's comment, I was able to gain some insight from the example he provided and therefore was able to do some more digging until I found this QOpenGLWidget's context is null post. The problem was me calling initializeGL in the constructor when the context was still NULL, so I removed the call since it would be called at some point when the window shows up.