本機接口提供對其擴展類的本機或特定平颱 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 ().
| 在 macOS 的 NSOpenGLContext 的本機接口 | |
| ELX 上下文的本機接口 | |
| GLX 上下文的本機接口 | |
| Windows WGL 上下文的本機接口 |
訪問透過 QOffscreenSurface::nativeInterface ().
| 在 Android 的離屏錶麵的本機界麵 |
訪問透過 QSGTexture::nativeInterface ().
| 提供對 Direct3D 11 紋理對象的訪問和啓用采納 | |
| Provides access to and enables adopting Direct3D 12 texture objects | |
| 提供對 Metal 紋理對象的訪問和啓用采納 | |
| 提供對 OpenGL 紋理對象的訪問和啓用采納 | |
| 提供對 Vulkan 圖像對象的訪問和啓用采納 |