本地接口
本地接口提供对其扩展类的本地或平台特定应用程序接口的访问。
这些接口位于QNativeInterface 名称空间中,涵盖的用例包括访问底层本地句柄、采用现有本地句柄或提供平台特定 API。
使用示例
访问底层本地句柄
在本地平台的某项功能未在 Qt 中公开的情况下,访问由 Qt 维护的本地句柄并使用这些句柄来调用本地 API 可能会有所帮助。
例如,通过QNativeInterface::QCocoaGLContext 本地接口访问 macOS 上QOpenGLContext 的底层 NSOpenGLContext:
using namespace QNativeInterface; if (auto *cocoaGLContext = glContext->nativeInterface<QCocoaGLContext>()) [cocoaGLContext->nativeContext() makeCurrentContext];
本机接口通过QOpenGLContext::nativeInterface() 访问器访问,该访问器会确保请求的接口可用,否则会返回nullptr
。然后通过nativeContext() 访问器访问底层 NSOpenGLContext。
采用现有本地句柄
与窗口嵌入用例类似,在某些情况下,本地平台或其他工具包创建了一个本地句柄,您希望将其传递给 Qt - 封装现有句柄而不是创建一个新句柄。
例如,采用现有的 NSOpenGLContext,并使用它与 Qt 创建的上下文共享资源:
using namespace QNativeInterface; QOpenGLContext *adoptedContext = QCocoaGLContext::fromNativeContext(nsOpenGLContext); anotherContext->setShareContext(adoptedContext);
采用的上下文由QNativeInterface::QCocoaGLContext 本地接口中的平台特定工厂函数创建。
访问平台特定的 API
在某些情况下,平台特定的 API 无法包含在跨平台 Qt 类中,但包含这些 API 仍然非常有用。访问这些 API 的方式与访问底层本地句柄的方式相同,即通过nativeInterface() 访问器,或直接作为本地接口中的静态函数。
例如,在 Windows 上提取 OpenGL 模块句柄:
using namespace QNativeInterface; HMODULE moduleHandle = QWGLContext::openGLModuleHandle();
源代码和二进制兼容性
本地接口 API 没有源代码或二进制兼容性保证,这意味着使用这些接口的应用程序只能保证在其开发时所使用的 Qt 版本上运行。
可用接口
有关所有可用接口的列表,请参阅QNativeInterface 命名空间。
QOpenGLContext
通过QOpenGLContext::nativeInterface() 访问。
macOS 上 NSOpenGLContext 的本地接口 | |
EGL 上下文的本地接口 | |
GLX 上下文的本地接口 | |
Windows 上 WGL 上下文的本地接口 |
QOffscreenSurface
通过QOffscreenSurface::nativeInterface() 访问。
安卓系统上屏幕外表面的本地接口 |
QSGTexture
通过QSGTexture::nativeInterface() 访问。
提供访问 Direct3D 11 纹理对象的权限,并可采用 Direct3D 11 纹理对象 | |
提供对 Direct3D 12 纹理对象的访问权限,并支持采用 Direct3D 12 纹理对象 | |
提供对金属纹理对象的访问权限并使其能够被采用 | |
提供访问 OpenGL 纹理对象的权限,并支持采用 OpenGL 纹理对象 | |
提供访问并启用 Vulkan 图像对象的权限 |
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.