Qt 作为应用程序开发的跨平台工具包的主要强项,是不需要为每个目标平台复制应用程序代码。
While Qt solves many of the typical tasks of writing an application, there are always corner cases that Qt can not cover, or where it makes more sense to build a feature on top of the platform specific APIs, or another toolkit.
To support these use-cases, while still allowing Qt to handle the bulk of the application logic, Qt provides a wide range of platform integration APIs, from simple type conversions to platform specific native interfaces.
Qt 的很多基本数据类型,譬如 QString , QPoint ,或 QImage ,提供到/来自本机等效类型的转换。
例如,在 Apple 平台获取当前用户的用户名:
NSProcessInfo *processInfo = NSProcessInfo.processInfo; QString userName = QString::fromNSString(processInfo.userName)
有关所有类型转换的完整列表,见 类型转换 概述。
Windows created by the underlying platform APIs may be used as both parent containers for Qt windows, or embedded into Qt windows as child windows.
The former is useful if the application is mainly written using the native platform APIs, but where parts of the application uses Qt, for example to draw a specialized UI. To embed Qt into the window hierarchy of the native application, use QWindow::winId () to get the native handle for the Qt window, and then use the native APIs to re-parent the window into the native UI.
The latter is useful if the native platform, or another toolkit, exposes a specialized control as a native window. By using QWindow::fromWinId () to wrap the native window handle in a QWindow , the window can then be re-parented into the Qt window hierarchy as any other QWindow 。要重新父级此 QWindow into a Qt Widget based UI, use the widgets-specific QWidget::createWindowContainer () 函数。
Qt 中的大多数事件处理用例均被跨平台事件交付充分覆盖,凭借 QWindow::event () 和好友,或透过 QObject::installEventFilter ().
若这不够,Qt 还提供了对本机事件交付的访问。可以安装全局事件过滤器以接收所有本机事件,通过使用 QCoreApplication::installNativeEventFilter (),且每个窗口的本机事件的处理可以在 QWindow::nativeEvent ().
注意: 干涉本机事件流,可能使 Qt 处于不一致状态。这些 API 应该主要用于增强 Qt 的现有事件处理 (例如:用于 Qt 尚无法处理的事件)。
Platform specific functionality not covered by the APIs mentioned above are handled by the more generic 本机接口 mechanism in Qt. The interfaces provide access to native or platform specific APIs of the classes they extend.
接口存活于 QNativeInterface namespace, and cover use-cases such as accessing underlying native handles, adopting existing native handles, or providing platform specific APIs.
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];
有关所有本机接口的完整列表,见 本机接口 概述。
警告: 本机 API 接口没有源代码或二进制兼容性保证,意味着使用这些接口的应用程序仅保证与针对它开发的 Qt 版本能一起工作。
In addition to the application developer APIs, Qt also interfaces with the platform when providing the underlying implementations of the cross-platform building blocks in Qt.
Examples are the event dispatcher abstractions in Qt Core and the rendering hardware abstractions in RHI.
这里的主要抽象层是 Qt Platform Abstraction , or QPA for short, which deals with window system integration and related use-cases.