轉載自:https://blog.csdn.net/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();
}
如果還出現問題,可以改變版本號試試