1

I am trying to add a QOpenGLWidget to my QGraphicsScene, but the application crashes when I call initializeOpenGLFunctions(). I am pretty sure that the context of OpenGLView is null and that is why it is crashing (provides no logs) for two reasons:

  1. When I print it, it outputs 0x0
  2. When I try to enable QOpenGLDebugLogger it outputs that there is no current context.

I thought that QOpenGLWidget would have an OpenGLContext out of the box. Any idea why the context is not getting set? Am I missing something in my initialization?

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();

header

class OpenGLView : public QOpenGLWidget, protected QOpenGLFunctions
{
}
#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) {
//    ...
}

4
  • Could be many things, like not having support for GL 3.2 Commented Nov 9, 2020 at 7:25
  • I do have support for GL 3.2. Been using OpenGL apart from QT with that version. Commented Nov 9, 2020 at 7:34
  • OpenGLView doesn't inherits QOpenGLFunctions. Where is that initializeOpenGLFunctions() function? Commented Nov 9, 2020 at 7:52
  • It does, I updated my code Commented Nov 9, 2020 at 7:58

1 Answer 1

2

It is because you called initializeGL() in the constructor. By that time, the context has not been initialized. The context is first initialized when the widget is shown. Details taken from Qt's source code below:

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This helps, I am now getting a non-null context, but still crashes when calling initializeOpenGLFunctions
What does the debugger say this time?
same thing "The program has unexpectedly finished."
Resolved it - there was a nullptr that took sometime to find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.