Jump to content

Qt for HarmonyOS/qtohosextras doc/Native Window ID Example

From Qt Wiki
Revision as of 02:40, 17 September 2026 by Shawn Luo (talk | contribs) (Publish example source listings and restore Files lists (QTFOROH-2307))

< Qt for HarmonyOS / qtohosextras doc

Native Window ID Example

Demonstrates obtaining the native OHOS window ID of a Qt window.


The main window has a button that creates additional windows. For every window the example calls QtOhosExtras::tryGetNativeWindowId() after the window has been shown and displays the result in the window title, or <invalid> when the returned pointer is null.

 void setWindowTitleToNativeId(QWidget *window)
 {
     auto nativeWindowId = QtOhosExtras::tryGetNativeWindowId(window->windowHandle());
     window->setWindowTitle("ID: " + (nativeWindowId != nullptr ? QString::number(*nativeWindowId) : "<invalid>"));
 }

Note: tryGetNativeWindowId() must only be called after the window has been shown, and the returned ID is not guaranteed to remain valid for the lifetime of the application.

Files: