由于显卡的差异,有时候在创建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();
}

运行后崩溃

vs 显示QOpenGLWidget崩溃

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

vs 显示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();
}

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

相关文章:

  • 2022-01-07
  • 2021-11-06
  • 2021-07-12
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-09-01
  • 2021-05-05
  • 2022-01-11
  • 2021-04-19
相关资源
相似解决方案