本机接口提供对其扩展类的本机或特定平台 API 的访问。
接口存活于 QNativeInterface namespace, and cover use-cases such as accessing underlying native handles, adopting existing native handles, or providing platform specific APIs.
In situations where a feature of the native platform is not exposed in Qt, it can be helpful to access the native handles maintained by Qt, and use those to call the native APIs instead.
For example, to access the underlying NSOpenGLContext of an QOpenGLContext 在 macOS,凭借 QNativeInterface::QCocoaGLContext 本机接口:
using namespace QNativeInterface; if (auto *cocoaGLContext = glContext->nativeInterface<QCocoaGLContext>()) [cocoaGLContext->nativeContext() makeCurrentContext];
本机接口的访问透过
QOpenGLContext::nativeInterface
() accessor, which ensures that the requested interface is available, and otherwise returns
nullptr
. The underlying NSOpenGLContext is then accessed through the
nativeContext
() accessor.
类似于 窗口嵌入 use-case, there are situations where the native platform, or another toolkit, has created a native handle that you would like to pass on to Qt — wrapping the existing handle instead of creating a new one.
For example, to adopt an existing NSOpenGLContext, and use that to share resources with a context created by Qt:
using namespace QNativeInterface; QOpenGLContext *adoptedContext = QCocoaGLContext::fromNativeContext(nsOpenGLContext); anotherContext->setShareContext(adoptedContext);
The adopted context is created by a platform specific factory function in the QNativeInterface::QCocoaGLContext native interface.
In some cases an API is too platform specific to be included in the cross platform Qt classes, but is still useful to include. These APIs are available either in the same way as when accessing the underlying native handles, through the nativeInterface () accessor, or directly as static function in the native interface.
例如,要在 Windows 取出 OpenGL 模块句柄:
using namespace QNativeInterface; HMODULE moduleHandle = QWGLContext::openGLModuleHandle();
本机 API 接口没有源代码或二进制兼容性保证,意味着使用这些接口的应用程序仅保证与针对它开发的 Qt 版本能一起工作。
有关所有可用接口的列表,见 QNativeInterface 名称空间。
访问透过 QOpenGLContext::nativeInterface ().
QNativeInterface::QCocoaGLContext | 在 macOS 的 NSOpenGLContext 的本机接口 |
QNativeInterface::QEGLContext | ELX 上下文的本机接口 |
QNativeInterface::QGLXContext | GLX 上下文的本机接口 |
QNativeInterface::QWGLContext | Windows WGL 上下文的本机接口 |
访问透过 QOffscreenSurface::nativeInterface ().
QNativeInterface::QAndroidOffscreenSurface | 在 Android 的离屏表面的本机界面 |
访问透过 QSGTexture::nativeInterface ().
QNativeInterface::QSGD3D11Texture | 提供对 Direct3D 11 纹理对象的访问和启用采纳 |
QNativeInterface::QSGMetalTexture | 提供对 Metal 纹理对象的访问和启用采纳 |
QNativeInterface::QSGOpenGLTexture | 提供对 OpenGL 纹理对象的访问和启用采纳 |
QNativeInterface::QSGVulkanTexture | 提供对 Vulkan 图像对象的访问和启用采纳 |