Can't set desired OpenGL version in QGLWidget

≯℡__Kan透↙ 提交于 2019-12-03 03:06:13

You should move the OpenGL queries down after mainView.show();. Before show() the OpenGL context has not been initialized.

Short version: update to Qt5, it's fixed there.

P.S And if you can use 5.4 you should probably use QtOpenGL... classes instead of QGL... ones.

Long version: So, in case anyone ever come across a problem like this one.

I tried to create NOT OpenGL 3.0 context with my Intel HD3000 on ubuntu 14.04 and Qt 4.8.6. I came up with this test code provided at the end of the answer. Tried creating 3.1, 1.2, 2.1.. etc. contexts Core Compatible.. but always end up with context->format().majorVersion() showing the requested version, and glGetString(GL_VERSION) showing 3.0.

After about 3 hours I noticed that by default Qt Creator uses Qt4 instead of recent Qt 5, installed on my system. And after I recompiled the project with Qt 5.2.1 exactly the same code started working as expected.

Hope this may help someone.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.show();

    QGLFormat glFormat;
    glFormat.setVersion(3, 1);
    glFormat.setProfile(QGLFormat::NoProfile);
    glFormat.setSampleBuffers(true);
    glFormat.setDefaultFormat(glFormat);
    glFormat.setSwapInterval(1);
    QGLWidget widget(glFormat);
    widget.makeCurrent();

    const QGLContext *context = widget.context();

    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        qWarning("Failed to initialize GLEW\n");
    }
    qDebug() << "Context valid: " << context->isValid();
    qDebug() << "Really used OpenGl: " << context->format().majorVersion() << "." << context->format().minorVersion();
    qDebug() << "OpenGl information: VENDOR:       " << (const char*)glGetString(GL_VENDOR);
    qDebug() << "                    RENDERDER:    " << (const char*)glGetString(GL_RENDERER);
    qDebug() << "                    VERSION:      " << (const char*)glGetString(GL_VERSION);
    qDebug() << "                    GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
    qDebug() << "endstuff\n";

    return a.exec();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!