Important: Please read the Qt Code of Conduct -

How to retrieve the QEGLNativeContext object?

  • Hello,

    I would like to use some advanced EGL/OpenGL functions like eglCreateImageKHR() in a QtQuick window.

    To do so I need a reference to the EGL display and the EGL context.

    Today I am able to retrieve the QOpenGLContext of my window with a connection to its openglContextCreated signal.

    Then I could retrieve the associated QEGLNativeContext with myQOpenGLContext->nativeHandle().
    If I understand well the documentation, myQOpenGLContext->nativeHandle() should be a QVariant of a QEGLNativeContext object.

    But I don't understand how to convert this QVariant to a QEGLNativeContext object.

    My target is an i.mx6Q with a yocto from december 2015 and wayland-egl as backend.

    I hope someone might help,


  • I have tried the following (using qvariant_cast) :
    qEglContext = qvariant_cast<QEGLNativeContext* >(context->nativeHandle());

    But at build time I get a static assertion failed error: Type is not registered.

  • Lifetime Qt Champion


    I haven't used it yet but from the documentation, that function returns a value type not a pointer. Try with qvariant_cast<QEGLNativeContext>(context->nativeHandle())

  • Thanks @SGaist!

    I have tried with the value type and the static cast seems to work (no error at build time).

    But now, I would like to use the eglCreateImageKHR() function.
    Following is what I have trieed so far:

    1 - I prepare my image attributes array (I might need to check those values later to ensure compatibility):

    EGLint eglImageAttributes[] = {EGL_WIDTH, m_window->width(), EGL_HEIGHT, m_window->height(), EGL_MATCH_FORMAT_KHR, EGL_FORMAT_RGBA_8888_KHR, EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};

    2 - Then I retrieve the QEGLNativeContext as you suggested:

    QEGLNativeContext qEglContext = qvariant_cast<QEGLNativeContext>(context->nativeHandle());

    3 - And finaly I make the call to eglCreateImageKHR():

    EGLImageKHR eglImageHandle = eglCreateImageKHR(qEglContext.display(), qEglContext.context(), EGL_GL_RENDERBUFFER_KHR, (EGLClientBuffer)m_window->OpenGLSurface, eglImageAttributes);

    4 - But it fails:

    eglCreateImageKHR returns EGL_NO_IMAGE_KHR and eglGetError() returns EGL_BAD_DISPLAY.

    According to the eglCreateImageKHR() documentation, eglCreateImageKHR() generates an EGL_BAD_DISPLAY error when the handle (qEglContext.display()) is not valid:

    If <dpy> is not the handle of a valid EGLDisplay object, the error EGL_BAD_DISPLAY is generated

    So, where am I wrong?
    My code is fired by the the onOpenglContextCreated() signal. Also, I am pretty sure I am using an EGL backend since at run-time the logs say "Using Wayland-EGL".
    Why qEglContext.display() seems to be invalid?

    Any suggestions?

  • Lifetime Qt Champion

    For that one, I'd recommend checking with the Wayland folks. I haven't used it yet so I can't be of help here, sorry.

  • Hi Colin,

    I'm working on something similar. Was this issue ever resolved? If so, do you mind sharing?