Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohoswindowutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qvariant.h>
7#include <QtGui/private/qguiapplication_p.h>
8#include <QtGui/private/qohoswindowhints_p.h>
9#include <QtGui/qcolor.h>
10#include <QtGui/qpa/qplatformintegration.h>
11#include <QtGui/qpa/qplatformscreen_p.h>
12#include <QtGui/qpa/qplatformwindow_p.h>
13#include <QtGui/qscreen.h>
14#include <QtGui/qwindow.h>
15#include <QtWidgets/qwidget.h>
16#include <optional>
17
19
20/*!
21 \namespace QtOhosAppKit::Window
22 \inmodule QtOhosAppKit
23 \brief Applies OpenHarmony-specific hints to a QWindow or QWidget.
24
25 Each hint is recorded on the passed QWindow or QWidget and consumed by the
26 platform plugin. A hint may be set on a QWidget before its window exists; it
27 is carried over to the window once the widget is shown.
28*/
29
30namespace QtOhosAppKit {
31
32namespace Window {
33
34/*!
35 Sets or unsets a given \a window as a floating window according to
36 \a showAsFloatWindow. The OpenHarmony window type is fixed at creation, so
37 this has to be called before the window is shown.
38
39 \code
40 auto window = std::make_unique<MainWindow>();
41 QtOhosAppKit::Window::setShowWindowAsFloatWindowHint(window.get(), true);
42 window->show();
43 \endcode
44*/
45void setShowWindowAsFloatWindowHint(QWindow *window, bool showAsFloatWindow)
46{
47 window->setProperty(QOhosWindowHints::floatWindowKey, QVariant::fromValue(showAsFloatWindow));
48}
49
50/*! \overload */
51void setShowWindowAsFloatWindowHint(QWidget *widget, bool showAsFloatWindow)
52{
53 widget->setProperty(QOhosWindowHints::floatWindowKey, QVariant::fromValue(showAsFloatWindow));
54}
55
56/*!
57 Changes the privacy mode of \a window according to \a privacyModeEnabled. In
58 privacy mode the window content cannot be captured or recorded. This requires
59 the ohos.permission.PRIVACY_WINDOW permission to be declared in the
60 application's module.json5.
61*/
62void setWindowPrivacyMode(QWindow *window, bool privacyModeEnabled)
63{
64 window->setProperty(QOhosWindowHints::privacyModeKey, QVariant::fromValue(privacyModeEnabled));
65}
66
67/*!
68 Sets a given \a color as the background color of the \a window inner native
69 layer.
70*/
71void setSurfaceBackgroundColor(QWindow *window, const QColor &color)
72{
73 window->setProperty(QOhosWindowHints::surfaceBackgroundColorKey, QVariant::fromValue(color));
74}
75
76/*!
77 \overload
78
79 Sets a given \a color as the background color of the \a widget inner native
80 layer.
81*/
82void setSurfaceBackgroundColor(QWidget *widget, const QColor &color)
83{
84 widget->setProperty(QOhosWindowHints::surfaceBackgroundColorKey, QVariant::fromValue(color));
85}
86
87/*!
88 Sets the corner \a radius of a \a window. The window has to be a sub or
89 floating window (for example a window with the Qt::Window, Qt::Popup or
90 Qt::Dialog flags, or one marked with setShowWindowAsFloatWindowHint()). The
91 \a radius must not be negative; there is no upper limit, though OHOS visually
92 clamps it to the maximum achievable value.
93*/
94void setWindowCornerRadius(QWindow *window, double radius)
95{
96 if (radius < 0.0) {
97 qWarning("QtOhosAppKit::Window::setWindowCornerRadius: radius must not be negative");
98 return;
99 }
100 window->setProperty(QOhosWindowHints::cornerRadiusKey, QVariant::fromValue(radius));
101}
102
103/*!
104 \overload
105
106 Sets the corner \a radius of a \a widget. The widget has to be a window on its
107 own (not embedded in a parent widget). The \a radius must not be negative.
108*/
109void setWindowCornerRadius(QWidget *widget, double radius)
110{
111 if (radius < 0.0) {
112 qWarning("QtOhosAppKit::Window::setWindowCornerRadius: radius must not be negative");
113 return;
114 }
115 widget->setProperty(QOhosWindowHints::cornerRadiusKey, QVariant::fromValue(radius));
116}
117
118/*!
119 Sets or unsets a given \a window to keep the screen on according to
120 \a keepScreenOn.
121*/
122void setWindowKeepScreenOn(QWindow *window, bool keepScreenOn)
123{
124 window->setProperty(QOhosWindowHints::keepScreenOnKey, QVariant::fromValue(keepScreenOn));
125}
126
127/*!
128 \overload
129
130 Sets or unsets a given \a widget to keep the screen on according to
131 \a keepScreenOn.
132*/
133void setWindowKeepScreenOn(QWidget *widget, bool keepScreenOn)
134{
135 widget->setProperty(QOhosWindowHints::keepScreenOnKey, QVariant::fromValue(keepScreenOn));
136}
137
138/*!
139 Enables or disables resizing of \a window by dragging its edges according to
140 \a dragResizable. Takes effect on sub windows and floating windows, but not on
141 the application's main window.
142*/
143void setWindowDragResizable(QWindow *window, bool dragResizable)
144{
145 window->setProperty(QOhosWindowHints::dragResizableKey, QVariant::fromValue(dragResizable));
146}
147
148/*!
149 \overload
150
151 Enables or disables resizing of \a widget by dragging its edges according to
152 \a dragResizable. The widget has to be a window on its own (not embedded in a
153 parent widget). Takes effect on sub windows and floating windows, but not on
154 the application's main window.
155*/
156void setWindowDragResizable(QWidget *widget, bool dragResizable)
157{
158 widget->setProperty(QOhosWindowHints::dragResizableKey, QVariant::fromValue(dragResizable));
159}
160
161/*!
162 Sets whether the application's main window restores its last geometry on
163 startup according to \a hint. WindowGeometryPersistenceHint::FollowSystemSetting
164 defers to the system default. This has to be called before the main window is
165 shown.
166*/
168{
169 std::optional<bool> enabled;
170 switch (hint) {
172 enabled = false;
173 break;
175 enabled = true;
176 break;
178 break;
179 }
180
181 using QOhosIntegration = QNativeInterface::Private::QOhosIntegration;
182 auto *integration = QGuiApplicationPrivate::platformIntegration();
183 integration->call<&QOhosIntegration::setMainWindowGeometryPersistenceEnabled>(enabled);
184}
185
186}
187
188std::optional<double> tryGetNativeWindowId(QWindow *window)
189{
190 auto *ohosWindow = window->nativeInterface<QNativeInterface::Private::QOhosWindow>();
191 if (!ohosWindow)
192 return {};
193
194 return ohosWindow->windowId();
195}
196
197std::optional<double> tryGetScreenDisplayId(QScreen *screen)
198{
199 auto *ohosScreen = screen->nativeInterface<QNativeInterface::Private::QOhosScreen>();
200 if (!ohosScreen)
201 return {};
202
203 return ohosScreen->displayId();
204}
205
206}
207
208QT_END_NAMESPACE
Combined button and popup list for selecting options.
void setWindowKeepScreenOn(QWindow *window, bool keepScreenOn)
Sets or unsets a given window to keep the screen on according to keepScreenOn.
void setSurfaceBackgroundColor(QWindow *window, const QColor &color)
Sets a given color as the background color of the window inner native layer.
void setShowWindowAsFloatWindowHint(QWindow *window, bool showAsFloatWindow)
Sets or unsets a given window as a floating window according to showAsFloatWindow.
void setWindowPrivacyMode(QWindow *window, bool privacyModeEnabled)
Changes the privacy mode of window according to privacyModeEnabled.
void setWindowCornerRadius(QWindow *window, double radius)
Sets the corner radius of a window.
void setMainWindowGeometryPersistenceHint(WindowGeometryPersistenceHint hint)
Sets whether the application's main window restores its last geometry on startup according to hint.
void setWindowDragResizable(QWindow *window, bool dragResizable)
Enables or disables resizing of window by dragging its edges according to dragResizable.
std::optional< double > tryGetNativeWindowId(QWindow *window)
std::optional< double > tryGetScreenDisplayId(QScreen *screen)