Qt for HarmonyOS/qtohosextras doc/Native Window ID Example: Difference between revisions
Add example documentation from qdoc output (QTFOROH-2307) |
Drop manual breadcrumb, MediaWiki renders it automatically (QTFOROH-2307) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
= Native Window ID Example = | = Native Window ID Example = | ||
| Line 8: | Line 6: | ||
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 <code><invalid></code> when the returned pointer is null. | 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 <code><invalid></code> when the returned pointer is null. | ||
<syntaxhighlight lang="cpp"> | |||
void setWindowTitleToNativeId(QWidget *window) | |||
{ | |||
auto nativeWindowId = QtOhosExtras::tryGetNativeWindowId(window->windowHandle()); | |||
window->setWindowTitle("ID: " + (nativeWindowId != nullptr ? QString::number(*nativeWindowId) : "<invalid>")); | |||
} | |||
</syntaxhighlight> | |||
'''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. | '''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. | ||
| Line 13: | Line 21: | ||
Files: | Files: | ||
* [[Qt_for_HarmonyOS/qtohosextras_doc/ | * [[Qt_for_HarmonyOS/qtohosextras_doc/Native_Window_ID_Example_-_main.cpp|nativewindowid/main.cpp]] | ||
* [[Qt_for_HarmonyOS/qtohosextras_doc/ | * [[Qt_for_HarmonyOS/qtohosextras_doc/Native_Window_ID_Example_-_nativewindowid.pro|nativewindowid/nativewindowid.pro]] | ||
[[Category:Qt for HarmonyOS]] | [[Category:Qt for HarmonyOS]] | ||
Latest revision as of 05:59, 17 September 2026
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: