vs 显示QOpenGLWidget崩溃

本文详细介绍了在创建QOpenGLWidget时遇到初始化失败的问题及解决方案。通过设置QSurfaceFormat的深度缓冲区大小、模板缓冲区大小、OpenGL版本和配置文件,可以解决因显卡差异导致的初始化失败问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:https://blog.youkuaiyun.com/GengWenhui123/article/details/86640525

由于显卡的差异,有时候在创建QOpenGLWidget时需要初始化显卡

第一次创建QOpenGLWidget失败

#include "QtGuiApplication2.h"
#include <QtWidgets/QApplication>
#include <QOpenGLWidget>
int main(int argc, char *argv[])
{
//     QSurfaceFormat format;
//     format.setDepthBufferSize(24);
//     format.setStencilBufferSize(8);
//     format.setVersion(4, 3);
//     format.setProfile(QSurfaceFormat::CoreProfile);
//     QSurfaceFormat::setDefaultFormat(format);
    QApplication a(argc, argv);
    QOpenGLWidget w;
    w.show();
    return a.exec();
}

运行后崩溃:

 

如果出现上述问题,可以尝试把注释打开在运行

 

源码如下:

#include "QtGuiApplication2.h"
#include <QtWidgets/QApplication>
#include <QOpenGLWidget>
int main(int argc, char *argv[])
{
     QSurfaceFormat format;
     format.setDepthBufferSize(24);
     format.setStencilBufferSize(8);
     format.setVersion(4, 3);
     format.setProfile(QSurfaceFormat::CoreProfile);
     QSurfaceFormat::setDefaultFormat(format);
    QApplication a(argc, argv);
    QOpenGLWidget w;
    w.show();
    return a.exec();
}

如果还出现问题,可以改变版本号试试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值