Jump to content

Qt for HarmonyOS/user development guide/floating window guide: Difference between revisions

From Qt Wiki
Created page with "'''English''' 中文 = Qt for HarmonyOS: Development Guide for Floating Window Functionality = == Introduction == This document provides detailed instructions on implementing the **floating window** feature in Qt for HarmonyOS applications. HarmonyOS offers native floating window capabilities. Through the **QtOhosExtras** module, Qt provides developers with interfaces for creating and managing Widge..."
 
edit display of bold words
 
Line 5: Line 5:
== Introduction ==
== Introduction ==


This document provides detailed instructions on implementing the **floating window** feature in Qt for HarmonyOS applications.
This document provides detailed instructions on implementing the '''floating window''' feature in Qt for HarmonyOS applications.


HarmonyOS offers native floating window capabilities. Through the **QtOhosExtras** module, Qt provides developers with interfaces for creating and managing Widget / QWindow instances based on HarmonyOS's floating window framework.
HarmonyOS offers native floating window capabilities. Through the '''QtOhosExtras''' module, Qt provides developers with interfaces for creating and managing Widget / QWindow instances based on HarmonyOS's floating window framework.


This guide explains the usage, implementation mechanism, and key code examples of the floating window feature in detail.
This guide explains the usage, implementation mechanism, and key code examples of the floating window feature in detail.
Line 21: Line 21:
Qt for HarmonyOS supports the creation of multiple window types:
Qt for HarmonyOS supports the creation of multiple window types:


# **Standard Main Window**
# '''Standard Main Window'''
#* Built on HarmonyOS main window capabilities
#* Built on HarmonyOS main window capabilities
#* Displayed within the application task stack
#* Displayed within the application task stack
#* Follows the standard application lifecycle
#* Follows the standard application lifecycle
# **Floating Window**
# '''Floating Window'''
#* Built on native HarmonyOS floating window capabilities
#* Built on native HarmonyOS floating window capabilities
#* Can be displayed on top of other applications
#* Can be displayed on top of other applications
Line 52: Line 52:
The floating window functionality in Qt for HarmonyOS has the following characteristics:
The floating window functionality in Qt for HarmonyOS has the following characteristics:


# **Underlying Integration**: Implemented based on HarmonyOS native floating window capabilities.
# '''Underlying Integration''': Implemented based on HarmonyOS native floating window capabilities.
# **Qt Object Interface**: Supports creation, destruction, and property configuration via standard Qt interfaces.
# '''Qt Object Interface''': Supports creation, destruction, and property configuration via standard Qt interfaces.
# **Content Rendering**: Uses the same content loading and painting mechanism as regular QWidgets.
# '''Content Rendering''': Uses the same content loading and painting mechanism as regular QWidgets.
# **Child Window Support**: Supports modal and non-modal child windows within floating windows.
# '''Child Window Support''': Supports modal and non-modal child windows within floating windows.


----
----
Line 91: Line 91:
The function operates on the HarmonyOS platform as follows:
The function operates on the HarmonyOS platform as follows:


# **Window Hint Setting**: Applies a floating window display hint to the specified QWidget.
# '''Window Hint Setting''': Applies a floating window display hint to the specified QWidget.
# **Underlying Mapping**: Maps the Qt window to a native HarmonyOS floating window.
# '''Underlying Mapping''': Maps the Qt window to a native HarmonyOS floating window.
# **Property Inheritance**: Preserves all standard properties and behaviors of the Qt window.
# '''Property Inheritance''': Preserves all standard properties and behaviors of the Qt window.
# **Lifecycle Management**: Floating windows follow the standard lifecycle of Qt objects.
# '''Lifecycle Management''': Floating windows follow the standard lifecycle of Qt objects.


----
----
Line 104: Line 104:
The key to implementing floating window functionality is:
The key to implementing floating window functionality is:


**You must call setShowWindowAsFloatWindowHint() before calling show() on the window.**
* You must call setShowWindowAsFloatWindowHint() before calling show() on the window.


----
----
Line 129: Line 129:
     win->show();
     win->show();
}
}
</syntaxhighlight>
</syntaxhighlight>'''Notes:'''
 
* The floating window configuration API '''must''' be called before show().
**Notes:**
* The floating window configuration API **must** be called before show().
* Floating windows can be sized and configured just like regular QWidgets.
* Floating windows can be sized and configured just like regular QWidgets.
* It is recommended to use Qt::WA_DeleteOnClose for automatic resource release.
* It is recommended to use Qt::WA_DeleteOnClose for automatic resource release.
Line 157: Line 155:
     dlg->show();
     dlg->show();
}
}
</syntaxhighlight>
</syntaxhighlight>'''Notes:'''
 
**Notes:**
* A floating window can act as a parent for child windows.
* A floating window can act as a parent for child windows.
* Both modal (exec()) and non-modal (show()) windows are supported.
* Both modal (exec()) and non-modal (show()) windows are supported.
Line 172: Line 168:
QT += core gui widgets ohosextras
QT += core gui widgets ohosextras
SOURCES += main.cpp
SOURCES += main.cpp
</syntaxhighlight>
</syntaxhighlight>'''Key Configuration Notes:'''
 
* The '''ohosextras''' module '''must''' be added to use floating window APIs.
**Key Configuration Notes:**
* The **ohosextras** module **must** be added to use floating window APIs.


----
----
Line 225: Line 219:


# Launch the application: The main window displays normally.
# Launch the application: The main window displays normally.
# Click the **Open OHOS floating window** button.
# Click the '''Open OHOS floating window''' button.
# Verify that the floating window appears in floating mode and can overlay other applications.
# Verify that the floating window appears in floating mode and can overlay other applications.
# Create child windows within the floating window and verify modal and non-modal behavior.
# Create child windows within the floating window and verify modal and non-modal behavior.
Line 238: Line 232:
When using the floating window feature, pay attention to HarmonyOS permission requirements:
When using the floating window feature, pay attention to HarmonyOS permission requirements:


* **Floating Window Permission**: The application must request the permission to display floating windows.
* '''Floating Window Permission''': The application must request the permission to display floating windows.
* **System-level Permissions**: Some scenarios may require system-level permissions.
* '''System-level Permissions''': Some scenarios may require system-level permissions.
* **User Authorization**: Manual user authorization is required on first use.
* '''User Authorization''': Manual user authorization is required on first use.


----
----

Latest revision as of 03:08, 30 January 2026

English 中文

Qt for HarmonyOS: Development Guide for Floating Window Functionality

Introduction

This document provides detailed instructions on implementing the floating window feature in Qt for HarmonyOS applications.

HarmonyOS offers native floating window capabilities. Through the QtOhosExtras module, Qt provides developers with interfaces for creating and managing Widget / QWindow instances based on HarmonyOS's floating window framework.

This guide explains the usage, implementation mechanism, and key code examples of the floating window feature in detail.


Functional Architecture Overview

When using Qt floating window functionality on HarmonyOS, you need to understand the following architectural principles.

Application Window Types

Qt for HarmonyOS supports the creation of multiple window types:

  1. Standard Main Window
    • Built on HarmonyOS main window capabilities
    • Displayed within the application task stack
    • Follows the standard application lifecycle
  2. Floating Window
    • Built on native HarmonyOS floating window capabilities
    • Can be displayed on top of other applications
    • Has an independent display layer

Window Hierarchy Relationships

Qt floating windows support complex hierarchy structures:

Window Type Parent-Child Support Usage Scenarios
Main Window Supports child windows Application main interface
Floating Window Supports child windows Floating tools, notification windows
Child Window Nestable Dialogs, pop-ups

Core Features

The floating window functionality in Qt for HarmonyOS has the following characteristics:

  1. Underlying Integration: Implemented based on HarmonyOS native floating window capabilities.
  2. Qt Object Interface: Supports creation, destruction, and property configuration via standard Qt interfaces.
  3. Content Rendering: Uses the same content loading and painting mechanism as regular QWidgets.
  4. Child Window Support: Supports modal and non-modal child windows within floating windows.

Qt API Details

setShowWindowAsFloatWindowHint()

void QtOhosExtras::setShowWindowAsFloatWindowHint(
    QWidget *widget,
    bool enable
);

This is the core floating window configuration API provided by Qt for HarmonyOS. It is used to set a specified Qt window to HarmonyOS floating window mode.


Parameter Description

Parameter Type Description
widget QWidget* The window object to be set as a floating window
enable bool true enables floating window mode; false sets it to normal window mode

Functional Mechanism

The function operates on the HarmonyOS platform as follows:

  1. Window Hint Setting: Applies a floating window display hint to the specified QWidget.
  2. Underlying Mapping: Maps the Qt window to a native HarmonyOS floating window.
  3. Property Inheritance: Preserves all standard properties and behaviors of the Qt window.
  4. Lifecycle Management: Floating windows follow the standard lifecycle of Qt objects.

Development Implementation

Core Implementation Principle

The key to implementing floating window functionality is:

  • You must call setShowWindowAsFloatWindowHint() before calling show() on the window.

Key Code Examples

1. Creating a Floating Window

// Core implementation for creating a floating window
void MainWindow::openFloatingWindow()
{
    // Create window instance
    SecondWindow *win = new SecondWindow();

    // Critical: Set to floating window mode (must be called before show())
    QtOhosExtras::setShowWindowAsFloatWindowHint(win, true);

    // Configure window properties
    win->setAttribute(Qt::WA_DeleteOnClose);
    win->resize(300, 300);

    // Show the window
    win->show();
}

Notes:

  • The floating window configuration API must be called before show().
  • Floating windows can be sized and configured just like regular QWidgets.
  • It is recommended to use Qt::WA_DeleteOnClose for automatic resource release.

2. Creating Child Windows Inside a Floating Window

// Create a modal dialog with the floating window as parent
void SecondWindow::modalWithParent()
{
    SimpleDialog dlg(this); // Parent is the floating window
    dlg.setWindowTitle("Modal with Parent");
    dlg.exec();
}

// Create a non-modal child window with the floating window as parent
void SecondWindow::nonModalWithParent()
{
    SimpleDialog *dlg = new SimpleDialog(this);
    dlg->setWindowTitle("Non-Modal with Parent");
    dlg->setAttribute(Qt::WA_DeleteOnClose);
    dlg->show();
}

Notes:

  • A floating window can act as a parent for child windows.
  • Both modal (exec()) and non-modal (show()) windows are supported.
  • Child windows follow standard Qt parent-child relationships and lifecycle management.

Project Configuration

# FloatWindowChilds.pro
QT += core gui widgets ohosextras
SOURCES += main.cpp

Key Configuration Notes:

  • The ohosextras module must be added to use floating window APIs.

Application Scenarios

Applicable Scenarios

  • Floating tools: Calculators, notepads, and other small utilities
  • System notifications: Message alerts, status display windows
  • Auxiliary functions: Screen recording controls, volume adjustment panels
  • Multi-task collaboration: Functional windows that need to be displayed alongside other apps

Basic Usage Pattern

void createFloatingWindow()
{
    // 1. Create window instance
    YourWindow *window = new YourWindow();

    // 2. Set as floating window
    QtOhosExtras::setShowWindowAsFloatWindowHint(window, true);

    // 3. Configure window properties
    window->setAttribute(Qt::WA_DeleteOnClose);
    window->resize(desiredWidth, desiredHeight);

    // 4. Show the window
    window->show();
}

Compilation and Testing

Compilation Steps

qmake FloatWindowChilds.pro && make

Testing and Verification

  1. Launch the application: The main window displays normally.
  2. Click the Open OHOS floating window button.
  3. Verify that the floating window appears in floating mode and can overlay other applications.
  4. Create child windows within the floating window and verify modal and non-modal behavior.
  5. Verify that the floating window supports drag-and-drop movement.

Development Notes

Floating Window Permissions

When using the floating window feature, pay attention to HarmonyOS permission requirements:

  • Floating Window Permission: The application must request the permission to display floating windows.
  • System-level Permissions: Some scenarios may require system-level permissions.
  • User Authorization: Manual user authorization is required on first use.

Related Resources

  • [1] - Qt QWidget Class Reference Documentation