本機接口

本機接口提供對其擴展類的本機或特定平颱 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.

訪問平颱特定 API

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

訪問透過 QOpenGLContext::nativeInterface ().

QNativeInterface::QCocoaGLContext

在 macOS 的 NSOpenGLContext 的本機接口

QNativeInterface::QEGLContext

ELX 上下文的本機接口

QNativeInterface::QGLXContext

GLX 上下文的本機接口

QNativeInterface::QWGLContext

Windows WGL 上下文的本機接口

QOffscreenSurface

訪問透過 QOffscreenSurface::nativeInterface ().

QNativeInterface::QAndroidOffscreenSurface

在 Android 的離屏錶麵的本機界麵

QSGTexture

訪問透過 QSGTexture::nativeInterface ().

QNativeInterface::QSGD3D11Texture

提供對 Direct3D 11 紋理對象的訪問和啓用采納

QNativeInterface::QSGD3D12Texture

Provides access to and enables adopting Direct3D 12 texture objects

QNativeInterface::QSGMetalTexture

提供對 Metal 紋理對象的訪問和啓用采納

QNativeInterface::QSGOpenGLTexture

提供對 OpenGL 紋理對象的訪問和啓用采納

QNativeInterface::QSGVulkanTexture

提供對 Vulkan 圖像對象的訪問和啓用采納