无法在QGLWidget中设置所需的OpenGL版本

Pri*_*tic 9 c++ opengl qt opengl-3

我正在尝试在Qt 4.8.2中使用QGLWidget.我注意到QGLWidget创建的默认上下文没有显示3.1以上的OpenGL的任何输出.Qt wiki有一个教程,演示如何使用OpenGL 3.3绘制一个简单的三角形.当我尝试运行教程时,我得到一个空白屏幕.如果我将OpenGL版本更改为3.1,我会得到预期的输出(红色三角形).

我的视频卡支持OpenGL 4.2,QGLFormat::openGLVersionFlags()在创建QGLWidget之前调用显示Qt检测到OpenGL 4.2和所有以前的桌面版本.

这是另一个最小的例子:

#include <QApplication>
#include <QGLWidget>
#include <QDebug>
#include <QtDeclarative/qdeclarativeview.h>

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

    qDebug() << "OpenGL Versions Supported: " << QGLFormat::openGLVersionFlags();

    QGLFormat qglFormat;
    qglFormat.setVersion(4,2);  // get expected output with (3,1) and below, else blank window
    qglFormat.setProfile(QGLFormat::CoreProfile);
    qglFormat.setSampleBuffers(true);

    QGLWidget* qglWidget = new QGLWidget(qglFormat);

    QString versionString(QLatin1String(reinterpret_cast<const char*>(glGetString(GL_VERSION))));
    qDebug() << "Driver Version String:" << versionString;
    qDebug() << "Current Context:" << qglWidget->format();

    QDeclarativeView mainView;
    mainView.setViewport(qglWidget);
    mainView.setSource(QString("helloworld.qml"));
    mainView.show();

    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

OpenGL Versions Supported:  QFlags(0x1|0x2|0x4|0x8|0x10|0x20|0x40|0x1000|0x2000|0x4000|0x8000|0x10000) 
Driver Version String: "4.2.0 NVIDIA 295.53" 
Current Context: QGLFormat(options QFlags(0x1|0x2|0x4|0x10|0x20|0x80|0x200|0x400) , plane  0 , depthBufferSize  24 , accumBufferSize  16 , stencilBufferSize  8 , redBufferSize  8 , greenBufferSize  8 , blueBufferSize  8 , alphaBufferSize  -1 , samples  4 , swapInterval  0 , majorVersion  4 , minorVersion  2 , profile  1 )  
Run Code Online (Sandbox Code Playgroud)

QFlags()第一行的枚举列表描述了支持的OpenGL版本.该列表显示我支持除OpenGL/ES版本之外的所有变体.第三行的QFlags()描述了格式选项(alpha通道,模板缓冲区等).

任何人都知道为什么QGLWidget不适用于任何> = 3.1的东西?我在Linux上,有一个Nvidia GT440,glxinfo显示它支持OpenGL 4.2.0.驱动程序版本打印在上面的示例输出中.我不太确定还有什么可以尝试的.

编辑:在编辑之前,我对问题的解释做了一些非常糟糕的错误/假设.问题仍然类似,但希望现在更有意义.对不起任何困惑.

Mor*_*bel 5

您应该将 OpenGL 查询移到 后mainView.show();show()在OpenGL上下文还没有初始化之前。