Beispiel für Fenstereinstellungen in Qt für HarmonyOS
Ändert die Plattformeinstellungen der Fenster einer Qt-Anwendung unter HarmonyOS.
Jedes Fenster des Beispiels ändert die Einstellungen seines eigenen Plattformfensters, des „ window.Window “, in dem das Fenster angezeigt wird. Alle Fenster können den Bildschirm eingeschaltet lassen und ihren Inhalt in Screenshots und Bildschirmaufnahmen ausblenden. Unterfenster können zudem abgerundete Ecken haben und durch Ziehen ihrer Ränder in der Größe angepasst werden.
Die Einstellungen werden über Methoden von ` window.Window` geändert: ` setWindowKeepScreenOn()`, ` setWindowPrivacyMode()`, ` setWindowCornerRadius() ` und ` enableDrag()`. Der Eckenradius wird in vp gemessen. Ein Fenster erhält sein ` window.Window ` von ` QNativeInterface::QHarmonyWindow ` auf dem Qt-Thread als ` QHarmonyJsObjectRef `-Handle. Jedes Steuerelement übergibt das Handle und den neuen Wert an den JS-Thread:
QHarmonyJsObjectRef WindowSettingsWindow::jsWindow() const
{
auto *harmonyWindow = windowHandle() ? windowHandle()->nativeInterface<QNativeInterface::QHarmonyWindow>() : nullptr;
return harmonyWindow ? harmonyWindow->jsWindow() : QHarmonyJsObjectRef();
}
void WindowSettingsWindow::setKeepScreenOn(bool keepScreenOn)
{
QHarmonyApplication::runOnJsThread(
[jsWindowRef = jsWindow(), keepScreenOn, log = QPointer<QTextEdit>(m_log)](QHarmonyJsContext &jsContext) {
callJsWindowSetter(jsContext, jsWindowRef, "setWindowKeepScreenOn", Napi::Boolean::New(jsContext.env(), keepScreenOn), log);
});
}
void WindowSettingsWindow::setPrivacyMode(bool privacyMode)
{
QHarmonyApplication::runOnJsThread(
[jsWindowRef = jsWindow(), privacyMode, log = QPointer<QTextEdit>(m_log)](QHarmonyJsContext &jsContext) {
callJsWindowSetter(jsContext, jsWindowRef, "setWindowPrivacyMode", Napi::Boolean::New(jsContext.env(), privacyMode), log);
});
}
void WindowSettingsWindow::setCornerRadius(double cornerRadius)
{
QHarmonyApplication::runOnJsThread(
[jsWindowRef = jsWindow(), cornerRadius, log = QPointer<QTextEdit>(m_log)](QHarmonyJsContext &jsContext) {
callJsWindowSetter(jsContext, jsWindowRef, "setWindowCornerRadius", Napi::Number::New(jsContext.env(), cornerRadius), log);
});
}
void WindowSettingsWindow::setDragResizable(bool dragResizable)
{
QHarmonyApplication::runOnJsThread(
[jsWindowRef = jsWindow(), dragResizable, log = QPointer<QTextEdit>(m_log)](QHarmonyJsContext &jsContext) {
callJsWindowSetter(jsContext, jsWindowRef, "enableDrag", Napi::Boolean::New(jsContext.env(), dragResizable), log);
});
}Im JS-Thread löst die Aufgabe das Handle auf und ruft die Methode auf. Jede der Methoden gibt ein Promise zurück. Die an das Promise angehängten Handler protokollieren das Ergebnis im Fenster. Sie senden den Text mit ` QNativeInterface::QHarmonyApplication::runOnQtThread()` an den Qt-Thread. Ein vom Aufruf ausgelöster Fehler wird auf dieselbe Weise protokolliert:
void showStatusInLog(QPointer<QTextEdit> log, QString text)
{
QHarmonyApplication::runOnQtThread(
[log, text]() {
if (log)
log->append(text);
});
}
void showFailureInLog(QPointer<QTextEdit> log, const QString &asyncCallName, const QString &errorText)
{
showStatusInLog(log, QStringLiteral("%1 failed: %2").arg(asyncCallName, errorText));
}
void attachPromiseHandlersReportingStatusInLog(Napi::Object asyncCallPromise, const QString &asyncCallName, QPointer<QTextEdit> log)
{
asyncCallPromise.Get("then").As<Napi::Function>().Call(
asyncCallPromise,
{
Napi::Function::New(
asyncCallPromise.Env(),
[log, asyncCallName](const Napi::CallbackInfo &) {
showStatusInLog(log, QStringLiteral("%1 succeeded").arg(asyncCallName));
}),
Napi::Function::New(
asyncCallPromise.Env(),
[log, asyncCallName](const Napi::CallbackInfo &info) {
showFailureInLog(log, asyncCallName, QString::fromStdString(info[0].ToString()));
}),
});
}
void callJsWindowSetter(
QHarmonyJsContext &jsContext, const QHarmonyJsObjectRef &jsWindowRef, const std::string &setterName,
Napi::Value argument, QPointer<QTextEdit> log)
{
const QString asyncCallName = QStringLiteral("%1()").arg(QString::fromStdString(setterName));
Napi::Env env(jsContext.env());
Napi::Object jsWindow(env, jsWindowRef.value(jsContext));
if (jsWindow.IsEmpty()) {
showFailureInLog(log, asyncCallName, QStringLiteral("the window is not shown in a window.Window"));
return;
}
try {
auto setterFunc = jsWindow.Get(setterName).As<Napi::Function>();
auto setterPromise = setterFunc.Call(jsWindow, {argument}).As<Napi::Object>();
attachPromiseHandlersReportingStatusInLog(setterPromise, asyncCallName, log);
} catch (const Napi::Error &error) {
showFailureInLog(log, asyncCallName, QString::fromUtf8(error.what()));
}
}setWindowPrivacyMode() erfordert die Berechtigung „ ohos.permission.PRIVACY_WINDOW “. Das Beispiel deklariert diese in seiner Datei „ CMakeLists.txt “ mit ` qt_add_harmonyos_permission()`:
qt_add_harmonyos_permission(harmonywindowsettings
NAME ohos.permission.PRIVACY_WINDOW
)Die Steuerelemente zeigen die aktuellen Einstellungen des Plattformfensters an. „ getWindowProperties() “ und „ getWindowCornerRadius() “ geben diese synchron zurück, daher liest das Beispiel sie mit „ QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait()“ aus, das das Ergebnis der Aufgabe zurückgibt:
struct PlatformWindowSettings
{
std::optional<bool> optKeepScreenOn;
std::optional<bool> optPrivacyMode;
std::optional<double> optCornerRadius;
QString errorText;
};
PlatformWindowSettings readPlatformWindowSettings(QHarmonyJsContext &jsContext, const QHarmonyJsObjectRef &jsWindowRef, bool subWindow)
{
Napi::Env env(jsContext.env());
Napi::Object jsWindow(env, jsWindowRef.value(jsContext));
if (jsWindow.IsEmpty()) {
return PlatformWindowSettings{
.errorText = QStringLiteral("the window is not shown in a window.Window"),
};
}
PlatformWindowSettings settings;
try {
auto getWindowPropertiesFunc = jsWindow.Get("getWindowProperties").As<Napi::Function>();
auto windowProperties = getWindowPropertiesFunc.Call(jsWindow, {}).As<Napi::Object>();
settings.optKeepScreenOn = windowProperties.Get("isKeepScreenOn").As<Napi::Boolean>().Value();
settings.optPrivacyMode = windowProperties.Get("isPrivacyMode").As<Napi::Boolean>().Value();
if (subWindow) {
auto getWindowCornerRadiusFunc = jsWindow.Get("getWindowCornerRadius").As<Napi::Function>();
settings.optCornerRadius = getWindowCornerRadiusFunc.Call(jsWindow, {}).As<Napi::Number>().DoubleValue();
}
} catch (const Napi::Error &error) {
settings.errorText = QString::fromUtf8(error.what());
}
return settings;
}Das Beispiel liest die Einstellungen jedes Mal, wenn das Fenster angezeigt wird, sobald es sichtbar ist, da das Plattformfenster zu diesem Zeitpunkt bereits angezeigt wird. Die Plattform gibt nicht an, ob ein Fenster durch Ziehen seiner Ränder in der Größe verändert werden kann, daher verwendet das Beispiel stattdessen den Anfangszustand dieses Kontrollkästchens:
void WindowSettingsWindow::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
windowHandle()->installEventFilter(this);
}
bool WindowSettingsWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == windowHandle() && event->type() == QEvent::Expose && windowHandle()->isExposed()) {
windowHandle()->removeEventFilter(this);
syncWithPlatformWindow();
}
return QWidget::eventFilter(watched, event);
}
void WindowSettingsWindow::syncWithPlatformWindow()
{
const QHarmonyJsObjectRef jsWindowRef = jsWindow();
const bool subWindow = isSubWindow();
const PlatformWindowSettings settings = QHarmonyApplication::runOnJsThreadAndWait(
[&](QHarmonyJsContext &jsContext) {
return readPlatformWindowSettings(jsContext, jsWindowRef, subWindow);
});
if (settings.optKeepScreenOn) {
const QSignalBlocker blocker(m_keepScreenOnCheckBox);
m_keepScreenOnCheckBox->setChecked(*settings.optKeepScreenOn);
}
if (settings.optPrivacyMode) {
const QSignalBlocker blocker(m_privacyModeCheckBox);
m_privacyModeCheckBox->setChecked(*settings.optPrivacyMode);
}
if (settings.optCornerRadius) {
const QSignalBlocker blocker(m_cornerRadiusSpinBox);
m_cornerRadiusSpinBox->setValue(*settings.optCornerRadius);
}
if (!settings.errorText.isEmpty())
m_log->append(QStringLiteral("reading the window settings failed: %1").arg(settings.errorText));
if (subWindow)
setDragResizable(m_dragResizableCheckBox->isChecked());
}Ausführen des Beispiels
Drücken Sie „ New sub window “, um ein Unterfenster zu öffnen. Ändern Sie dessen Eckenradius oder deaktivieren Sie „ Resizable by dragging its edges “ und versuchen Sie, die Größe des Unterfensters durch Ziehen eines Randes zu ändern.
Um „ Keep the screen on “ zu überprüfen, legen Sie in den Systemeinstellungen eine kurze Bildschirm-Timeout-Zeit fest. Solange die Option aktiviert ist und das Fenster angezeigt wird, bleibt der Bildschirm eingeschaltet.
Um „ Hide the content in screenshots and recordings “ zu überprüfen, aktivieren Sie die Option und erstellen Sie einen Screenshot. Der Inhalt des Fensters erscheint darin nicht.
Der Eckenradius und die Größenänderung durch Ziehen hängen vom Gerät ab. Auf einem Gerät, das diese Funktionen nicht unterstützt, schlagen die Aufrufe fehl und das Fenster protokolliert den Fehler.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.