无法为 QOpenGLWidget 启用调试上下文

Can't enable debug context for QOpenGLWidget

本文关键字:调试 上下文 启用 QOpenGLWidget      更新时间:2023-10-16

我想在我的Qt QOpenGLWidget应用程序中启用OpenGL日志记录。

主.cpp

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setOption(QSurfaceFormat::DebugContext);
app.setApplicationName("MPGLES");
app.setApplicationVersion("0.0.1");
MainWidget widget;
widget.setFormat(format);
widget.show();
return app.exec();
}

主小部件.cpp

MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent), m_debugLogger(Q_NULLPTR) { }
void MainWidget::initializeGL()
{
makeCurrent();
m_functions.initializeOpenGLFunctions();
m_functions.glClearColor(0, 0, 0, 1);
m_debugLogger = new QOpenGLDebugLogger(context());
if (m_debugLogger->initialize()) {
qDebug() << "GL_DEBUG Debug Logger" << m_debugLogger << "n";
connect(m_debugLogger, SIGNAL(messageLogged(QOpenGLDebugMessage)), this, SLOT(messageLogged(QOpenGLDebugMessage)));
m_debugLogger->startLogging();
}
initializeMP();
timer.start(12, this);
}

通话后

m_debugLogger->initialize();

我看到下一个输出:

QOpenGLDebugLogger::initialize(): the current context is not a debug context:
this means that the GL may not generate any debug output at all.
To avoid this warning, try creating the context with the
QSurfaceFormat::DebugContext surface format option.

为什么我不能创建一个QOpenGLDebugLogger?我的代码出了什么问题?

您需要在使用 QOpenGLDebugLogger 之前创建 QOpenGLContext。

就像:

QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setVersion(3, 3);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setOption(QSurfaceFormat::DebugContext);
QSurfaceFormat::setDefaultFormat(format);
QOpenGLContext *context = new QOpenGLContext();
context->setFormat(format);
context->create();