平台集成

Qt's main strength as a cross-platform toolkit for application development is removing the need for duplicating the application code for each target platform.

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 . To re-parent this QWindow into a Qt Widget based UI, use the widgets-specific QWidget::createWindowContainer () 函数。

事件处理

Most event handling use-cases in Qt are sufficiently covered by the cross platform event delivery, via QWindow::event () and friends, or through QObject::installEventFilter ().

In cases where this is not enough, Qt provides access to the delivery of the native events. A global event filter that receives all native events can be installed by using QCoreApplication::installNativeEventFilter (), while per-window native events can be handled in QWindow::nativeEvent ().

注意: Interfering with the native event flow may put Qt in an inconsistent state. These APIs should primarily be used to augment Qt's existing event handling, for example for events Qt doesn't handle yet.

本机接口

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.

The interfaces live in the 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 on macOS, via the QNativeInterface::QCocoaGLContext 本机接口:

using namespace QNativeInterface;
if (auto *cocoaGLContext = glContext->nativeInterface<QCocoaGLContext>())
    [cocoaGLContext->nativeContext() makeCurrentContext];
							

有关所有本机接口的完整列表,见 本机接口 概述。

警告: There are no source or binary compatibility guarantees for the native interface APIs, meaning that an application using these interfaces is only guaranteed to work with the Qt version it was developed against.

平台支持

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.

The main abstraction layer here is the Qt Platform Abstraction , or QPA for short, which deals with window system integration and related use-cases.