Qt QOpenGLWidget';的上下文为空
我正试图向我的Qt QOpenGLWidget';的上下文为空,qt,opengl,qml,Qt,Opengl,Qml,我正试图向我的qgraphicscene中添加QOpenGLWidget,但当我调用InitializePenglFunctions()时,应用程序崩溃。我非常确定OpenGLView的上下文是空的,这就是为什么它会崩溃(不提供日志),原因有两个: 当我打印它时,它输出0x0 当我尝试启用QOpenGLDebugLogger时,它输出没有当前上下文 我以为QOpenGLWidget会有一个现成的OpenGLContext。你知道为什么上下文没有设置好吗?我在初始化过程中遗漏了什么吗 QSurfa
qgraphicscene
中添加QOpenGLWidget
,但当我调用InitializePenglFunctions()
时,应用程序崩溃。我非常确定OpenGLView
的上下文是空的,这就是为什么它会崩溃(不提供日志),原因有两个:
0x0
QOpenGLDebugLogger
时,它输出没有当前上下文QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setOption(QSurfaceFormat::DebugContext);
QSurfaceFormat::setDefaultFormat(format);
OpenGLView view = new OpenGLView();
标题
class OpenGLView : public QOpenGLWidget, protected QOpenGLFunctions
{
}
这是因为您在构造函数中调用了
initializeGL()
。到那时,上下文尚未初始化。当显示小部件时,首先初始化上下文。详情如下:
void QOpenGLWidgetPrivate::initialize()
{
Q_Q(QOpenGLWidget);
如果(已初始化)
返回;
...
QScopedPointer ctx(新QOpenGLContext);
ctx->setFormat(requestedFormat);
如果(共享上下文){
ctx->setShareContext(共享上下文);
ctx->setScreen(shareContext->screen());
}
如果(不太可能(!ctx->create()){
qWarning(“QOpenGLWidget:未能创建上下文”);
返回;
}
...
context=ctx.take();
初始化=真;
q->initializeGL();
}
bool QOpenGLWidget::事件(QEvent*e)
{
Q_D(QOpenGLWidget);
开关(e->type()){
...
case QEvent::Show://重新租赁可能不会导致调整大小,因此在展会上也会重新调整
如果(d->initialized&&window()->windowHandle())
&&d->context->shareContext()!=QWidgetPrivate::get(window())->shareContext()
{
//特例:为隐藏的小部件创建grabFramebuffer(),然后该小部件变为可见。
//重新创建所有资源,因为上下文现在需要与TLW共享。
if(!QCoreApplication::testAttribute(Qt::AA_ShareOpenGLContexts))
d->reset();
}
如果(!d->initialized&&!size().isEmpty()&&window()->windowHandle()){
d->initialize();
如果(d->初始化)
d->重新创建FBO();
}
打破
...
}
返回QWidget::event(e);
}
可能有很多事情,比如不支持GL 3.2,我确实支持GL 3.2。在该版本中,除了QT之外,还一直在使用OpenGL。
OpenGLView
没有继承QOpenGLFunctions
。initializePenglFunctions()
函数在哪里?它在哪里,我更新了我的代码这很有帮助,我现在得到了一个非空上下文,但在调用initializePenglFunctions时仍然崩溃调试器这次说了什么?同样的事情“程序意外完成了”。解决了它-有一个nullptr需要一些时间才能找到。
#include "OpenGLView.h"
OpenGLView::OpenGLView(QWidget *parent) : QOpenGLWidget(parent) {
initializeGL();
}
void OpenGLView::initializeGL() {
initializeOpenGLFunctions(); // crashes
// ...
}
void OpenGLView::paintGL() {
// ...
}
void OpenGLView::resizeGL(int w, int h) {
// ...
}
void QOpenGLWidgetPrivate::initialize()
{
Q_Q(QOpenGLWidget);
if (initialized)
return;
...
QScopedPointer<QOpenGLContext> ctx(new QOpenGLContext);
ctx->setFormat(requestedFormat);
if (shareContext) {
ctx->setShareContext(shareContext);
ctx->setScreen(shareContext->screen());
}
if (Q_UNLIKELY(!ctx->create())) {
qWarning("QOpenGLWidget: Failed to create context");
return;
}
...
context = ctx.take();
initialized = true;
q->initializeGL();
}
bool QOpenGLWidget::event(QEvent *e)
{
Q_D(QOpenGLWidget);
switch (e->type()) {
...
case QEvent::Show: // reparenting may not lead to a resize so reinitalize on Show too
if (d->initialized && window()->windowHandle()
&& d->context->shareContext() != QWidgetPrivate::get(window())->shareContext())
{
// Special case: did grabFramebuffer() for a hidden widget that then became visible.
// Recreate all resources since the context now needs to share with the TLW's.
if (!QCoreApplication::testAttribute(Qt::AA_ShareOpenGLContexts))
d->reset();
}
if (!d->initialized && !size().isEmpty() && window()->windowHandle()) {
d->initialize();
if (d->initialized)
d->recreateFbo();
}
break;
...
}
return QWidget::event(e);
}