Important: Please read the Qt Code of Conduct -

QtPlatformHeaders and QCocoaNativeContext

  • Greetings everyone. I'm needing to get the nativeHandle for a QOpenGLWidget in order to set up initialisation for some OpenCL (note: CL, not GL) functions. As I am on Mac, it seems I need to include some specific Qt headers.

    #include <QtPlatformHeaders/QCocoaNativeContext>

    However, in QtCreator it doesn't seem to be finding these specific platform headers.
    I can confirm my Qt installation contains them at: (MyQtDir)/5.12.1/clang_64/include/QtPlatformHeaders/

    The following page:
    indicates :-

    As the module is header-only, no further modifications to the .pro files are required to use it.
    Note: The module name (QtPlatformHeaders) must appear in the #include directive.
    Note: It is not necessary to enclose the code in #ifdef directives depending on platform.

    Sadly I'm at something of a loss as to how to resolve this.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Doesnt winId fill the bill ?

  • Hi @SGaist , thanks for the welcome!

    Unfortunately, I need to access the native context (a special struct dependent on platform). I'm able to use OpenCL fine in Qt at the moment, but it's much faster if I can use the GL/CL interoperability instead of manually storing pixel data, running OpenCL on it, then uploading it to the GPU again.

    The closest topic I can find is this one:
    Provide OpenGl context to a OpenCL context

    From the code section in that posting:

    QVariant nativeHandle = context->nativeHandle();
    if (!nativeHandle.isNull() && nativeHandle.canConvert<QWGLNativeContext>()) {
        QWGLNativeContext nativeContext = nativeHandle.value<QWGLNativeContext>();
        HGLRC hglrc = nativeContext.context();

    Where @wwolff has used QWGNNativeContext for Windows, I would need to use QCocoaNativeContext for Mac. At that point, I think it's then used as CGLContextObj to obtain the share group, etc. for linking OpenGL and OpenCL.

    Both of these are structs are declared in the Qt/<version>/<compiler>/include/QtPlatformHeaders/ include path, which very strangely doesn't seem to be accessible from QtCreator. I've not succeeded in finding a way to get Qt to see this path.

    (in that post you did point to a QtOpenCL implementation which I'm looking at now for clues!)

  • Hi @SGaist , I actually found the code I was looking for in that old QtOpenCL project you linked. Turns out to be a lot simpler than I had originally thought! Very portable too.

    Here it is in case anyone searches in future:

        if(CGLContextObj context = CGLGetCurrentContext()) {
            if(CGLShareGroupObj shareGroup = CGLGetShareGroup(context)) {
                cl_context_properties properties[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, cl_context_properties(shareGroup), 0 };
                auto appleCLContext = cl::Context(CL_DEVICE_TYPE_ALL, properties);
                auto availableDevices = appleCLContext.getInfo<CL_CONTEXT_DEVICES>();
                for(auto& d : availableDevices) {
                    qDebug() << d.getInfo<CL_DEVICE_NAME>().c_str();

    NOTE: This method is specific to Apple which uses sharegroups to initialise OpenCL/OpenGL interoperability. The Windows and Linux methods are different.

Log in to reply