关于 Qt使用QOpenGLWidget运行时崩溃 的解决方法

若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/115421404
长期持续带来更多项目与技术分享,咨询请加QQ:21497936、微信:yangsir198808

红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)

Qt开发专栏:各种问题解决(点击传送门)


问题

  使用QOpenGLWidget窗口崩溃。


问题源码

#include <QApplication>
#include <QWidget>
#include <QOpenGLWidget>

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

    QOpenGLWidget w;
    w.show();

    return a.exec();
}

解决

  添加设置opengl版本的源码,修改后源码如下:

#include <QApplication>
#include <QWidget>
#include <QOpenGLWidget>

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

    QSurfaceFormat format;
//    format.setDepthBufferSize(24);
//    format.setStencilBufferSize(8);
    format.setVersion(4, 3);    // 关键是这一句,设置opengl版本号
//    format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);
    QOpenGLWidget w;
    w.show();

    return a.exec();
}

测试成功

  在这里插入图片描述


若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/115421404

猜你喜欢

转载自blog.csdn.net/qq21497936/article/details/115421404