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