平颱集成

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.