I use Qt to create a 3D program with GLEW. I have a problem when I call glewInit() from run-time creating Widget. I create an inherited class , MyRender, based on QOpenGLWidget. Then, implemented initializeGL() with
GLenum err;
if( err = glewInit() )
{
printf( "error because: %s\n", glewGetErrorString( err ) );
exit(123);
}
Normally, I use class MyRender via Qt Designer and promote QOpenGLWidget to MyRender. Then I will have MyRender Object since the program starts. There is no problem.
However, when I create MyRender at Run-time. For example;
MyRender * myrender = new MyRender ( this );
The program will crash when calling glewInit()
Missing GL version // glewInit() problem
I found the people who have the same problem as me from this
However, in the post, people use either GLUT or SDL to create context. Since I use only GLEW how could I make the context from QOpenglWidget the same way as
glutInitDisplayMode(GLUT_RGB); //use GLUT
or
sf::Window App(sf::VideoMode(400, 400, 32), "Window"); //use SDL
or
glfwMakeContextCurrent // use glfw
Since I use none of them. I use only QOpenGLWidget and glew. I tried with
myrender->makeCurrent(); // fail
myrender->initializeGL(); // fail
before calling
glewInit()
however, the problem still persists.
About my machine: I use Windows 10 64-bit. Qt 5.11 GLEW 2.1.0
EDIT:
I test my code with
void initializeGL()
{
echo("inside initializeGL");
QOpenGLContext* current = this->context();
if( current == nullptr )
{
printf("current context is null\n");
exit(123);
}
else
{
printf("current context is good\n");
}
GLenum err = glewInit(); // error here
...
}
If I use Qt Designer to promote openGLWidget to MyRender . the context will be OK; However, if I create MyRender in Run-time
MyRender* myrender = new MyRender( this );
The context will be null and leads glewInit() error.