You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10+Qt5.12.3下OpenGLES上下文创建失败求助

问题:Windows10下Qt5.12.3使用OpenGLES创建QOpenGLWidget上下文失败

在Windows10桌面平台基于Qt5.12.3开发,继承QOpenGLWidget实现了GraphicsViewer类。当设置QSurfaceFormat::renderableType为QSurfaceFormat::OpenGLES时,Widget无法绘制内容,且输出以下错误:

QOpenGLWidget: Failed to create context
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: QOpenGLContext creation failed
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed

相关代码如下:

main.cpp

#include "QtOpenGLES.h"
#include <QApplication>
#include <QSurfaceFormat>

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

    QSurfaceFormat f;
    f.setVersion(2, 0);
    f.setRenderableType(QSurfaceFormat::OpenGLES);

    QSurfaceFormat::setDefaultFormat(f);

    QtOpenGLES w;
    w.show();

    return a.exec();
}

QtOpenGLES.h

#ifndef QTOPENGLES_H
#define QTOPENGLES_H

#include <QMainWindow>

namespace Ui {
class QtOpenGLES;
}

class QtOpenGLES : public QMainWindow
{
    Q_OBJECT

public:
    explicit QtOpenGLES(QWidget *parent = nullptr);
    ~QtOpenGLES();

private:
    Ui::QtOpenGLES *ui;
};

#endif // QTOPENGLES_H

QtOpenGLES.cpp

#include "QtOpenGLES.h"
#include "ui_QtOpenGLES.h"

QtOpenGLES::QtOpenGLES(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QtOpenGLES)
{
    ui->setupUi(this);
}

QtOpenGLES::~QtOpenGLES()
{
    delete ui;
}

GraphicsViewer.h

#ifndef GRAPHICSVIEWER_H
#define GRAPHICSVIEWER_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>

class GraphicsViewer : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    GraphicsViewer(QWidget* parent = nullptr);

protected:
    void initializeGL();
    void paintGL();
};

GraphicsViewer.cpp

#include "GraphicsViewer.h"

#include <QtOpenGL>

GraphicsViewer::GraphicsViewer(QWidget *parent) :
    QOpenGLWidget (parent)
{

}

void GraphicsViewer::initializeGL()
{
    initializeOpenGLFunctions();
}

void GraphicsViewer::paintGL()
{
    glClearColor(1, 0, 0, 1);

}
解决方案

1. 确认Qt版本是否支持OpenGLES

Qt5.12.3的Windows桌面默认发行版通常基于桌面OpenGL编译,不包含OpenGLES支持。你需要:

  • 下载Qt的WinRT版本(针对Windows平台的GLES支持),或者
  • 自行编译Qt,编译时添加配置参数-opengl es2启用OpenGLES 2.0支持。

2. 替换OpenGL函数类为GLES专用版本

QOpenGLFunctions默认绑定桌面OpenGL接口,换成对应GLES版本的函数类:
修改GraphicsViewer.h:

#include <QOpenGLWidget>
#include <QOpenGLFunctions_ES2>  // 替换为GLES2专用函数类

class GraphicsViewer : public QOpenGLWidget, protected QOpenGLFunctions_ES2
{
    Q_OBJECT

public:
    GraphicsViewer(QWidget* parent = nullptr);

protected:
    void initializeGL();
    void paintGL();
};

3. 完善QSurfaceFormat配置

添加必要的缓冲区配置,提升上下文创建成功率:
修改main.cpp中的格式设置:

QSurfaceFormat f;
f.setVersion(2, 0);
f.setRenderableType(QSurfaceFormat::OpenGLES);
f.setProfile(QSurfaceFormat::NoProfile);  // GLES不需要profile
f.setDepthBufferSize(24);
f.setAlphaBufferSize(8);
QSurfaceFormat::setDefaultFormat(f);

4. 修复paintGL的绘制逻辑

当前paintGL仅设置清屏颜色,未执行清屏操作,即使上下文创建成功也看不到效果:
修改GraphicsViewer.cpp的paintGL:

void GraphicsViewer::paintGL()
{
    glClearColor(1, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);  // 添加清屏调用
}

5. 排除UI容器影响

暂时跳过MainWindow,直接在main中创建GraphicsViewer,验证上下文是否能正常创建:
修改main.cpp:

#include "GraphicsViewer.h"
#include <QApplication>
#include <QSurfaceFormat>

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

    QSurfaceFormat f;
    f.setVersion(2, 0);
    f.setRenderableType(QSurfaceFormat::OpenGLES);
    f.setProfile(QSurfaceFormat::NoProfile);
    QSurfaceFormat::setDefaultFormat(f);

    GraphicsViewer w;
    w.show();

    return a.exec();
}

6. 更新显卡驱动

Windows平台的OpenGLES支持依赖显卡驱动,确保你的显卡驱动是最新版本,部分老驱动对GLES的支持不完善。


内容的提问来源于stack exchange,提问作者iammetehan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 05:54:40