【问题标题】:Qt QOpenGLWidget crash when call glewInit() with error : Missing GL version调用 glewInit() 时 Qt QOpenGLWidget 崩溃并出现错误:缺少 GL 版本
【发布时间】:2019-09-01 15:00:57
【问题描述】:

我使用 Qt 通过 GLEW 创建一个 3D 程序。 当我从运行时创建小部件时调用 glewInit() 时遇到问题。 我基于 QOpenGLWidget 创建了一个继承类 MyRender。然后,用

实现 initializeGL()
GLenum err;
if( err = glewInit() )
{
    printf( "error because: %s\n", glewGetErrorString( err )  );
    exit(123);
}

通常,我通过 Qt Designer 使用类 MyRender 并将 QOpenGLWidget 提升为 MyRender。然后我将拥有 MyRender 对象,因为程序启动。没有问题。

但是,当我在运行时创建 MyRender 时。例如;

MyRender * myrender = new MyRender ( this );

调用glewInit()时程序会崩溃

Missing GL version   // glewInit() problem

我从this找到和我有同样问题的人

但是,在帖子中,人们使用 GLUT 或 SDL 来创建上下文。由于我只使用 GLEW,我怎么能以与 QOpenglWidget 相同的方式制作上下文

glutInitDisplayMode(GLUT_RGB); //use GLUT 

sf::Window   App(sf::VideoMode(400, 400, 32), "Window"); //use SDL 

glfwMakeContextCurrent    // use glfw

因为我没有使用它们。我只使用 QOpenGLWidget 和 glew。我试过了

myrender->makeCurrent(); // fail
myrender->initializeGL(); // fail

打电话之前

glewInit()

但是,问题仍然存在。

关于我的机器:我使用的是 Windows 10 64 位。 Qt 5.11 GLEW 2.1.0

编辑:

我用

测试我的代码
 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
        ...
 }

如果我使用 Qt Designer 将 openGLWidget 推广到 MyRendercontext 就可以了; 但是,如果我在运行时创建 MyRender

MyRender* myrender = new MyRender( this );

上下文将为空并导致 glewInit() 错误。

【问题讨论】:

    标签: qt opengl 3d glew qopenglwidget


    【解决方案1】:

    我发现问题是没有创建上下文,而当程序调用函数initializeGL()。我所做的解决方案是调用 show();在创建小部件之后。

    myrender = new MyRender( ui->mdiArea );
    myrender->show(); // this line make the context for QOpenGLWidget I think..
    
    ... 
    // note class MyRender is an inherited class from QOpenGLWidget
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      相关资源
      最近更新 更多