Beispiel für Keyframes beim Ziehen des Fensters in Qt für HarmonyOS
Legt die Keyframe-Richtlinie für die Größenänderung des Fensters einer Qt-Anwendung unter HarmonyOS fest.
Während der Benutzer die Größe eines Fensters durch Ziehen am Rand ändert, kann die Plattform die neue Größe nur an Keyframes melden, anstatt kontinuierlich. Die Keyframe-Richtlinie des Fensters aktiviert diesen Modus und legt fest, wann die Keyframes erfolgen. Das Beispiel legt die Richtlinie mit der Methode „ setDragKeyFramePolicy() “ des „ window.Window “ fest.
Die Richtlinie wird auf dem Qt-Thread als ` QJsonObject` erfasst, wobei nur die festgelegten Parameter enthalten sind. Das Fenster erhält sein ` window.Window ` über ` QNativeInterface::QHarmonyWindow ` auf dem Qt-Thread als ` QHarmonyJsObjectRef `-Handle und übergibt beides an den JS-Thread:
QJsonObject KeyFramePolicyWindow::makePolicy() const
{
QJsonObject policy{{QStringLiteral("enable"), m_enableCheckBox->isChecked()}};
if (!m_enableCheckBox->isChecked())
return policy;
for (const PolicyParameter ¶meter : m_policyParameters) {
if (parameter.setCheckBox->isChecked())
policy.insert(parameter.key, parameter.valueSpinBox->value());
}
return policy;
}
void KeyFramePolicyWindow::applyPolicy()
{
auto *harmonyWindow = windowHandle() ? windowHandle()->nativeInterface<QNativeInterface::QHarmonyWindow>() : nullptr;
const auto jsWindowRef = harmonyWindow ? harmonyWindow->jsWindow() : QHarmonyJsObjectRef();
const QJsonObject policy = makePolicy();
m_log->append(QStringLiteral("requested policy: %1").arg(toCompactJson(policy)));
QHarmonyApplication::runOnJsThread(
[jsWindowRef, policy, log = QPointer<QTextEdit>(m_log)](QHarmonyJsContext &jsContext) {
applyDragKeyFramePolicy(jsContext, jsWindowRef, policy, log);
});
}Im JS-Thread ordnet die Aufgabe die Richtlinie mit ` QtHarmony::mapQJsonObjectToJsObject()` einem JS-Objekt zu und ruft ` setDragKeyFramePolicy()` auf. Das zurückgegebene Promise wird zu der aktuell gültigen Richtlinie aufgelöst. Der Handler ordnet diese Richtlinie mit ` QtHarmony::mapJsObjectToQJsonObject()` wieder zu und sendet sie mit ` QNativeInterface::QHarmonyApplication::runOnQtThread()` an den Qt-Thread, wo das Fenster sie protokolliert. Eine Ablehnung oder ein Fehler, den die Aufrufe auslösen, 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 applyDragKeyFramePolicy(
QHarmonyJsContext &jsContext, const QHarmonyJsObjectRef &jsWindowRef, const QJsonObject &policy,
QPointer<QTextEdit> log)
{
const QString asyncCallName = QStringLiteral("setDragKeyFramePolicy()");
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 {
Napi::Object jsPolicy(env, QtHarmony::mapQJsonObjectToJsObject(env, policy));
auto setDragKeyFramePolicyFunc = jsWindow.Get("setDragKeyFramePolicy").As<Napi::Function>();
auto setDragKeyFramePolicyPromise = setDragKeyFramePolicyFunc.Call(jsWindow, {jsPolicy}).As<Napi::Object>();
setDragKeyFramePolicyPromise.Get("then").As<Napi::Function>().Call(
setDragKeyFramePolicyPromise,
{
Napi::Function::New(
env,
[log, asyncCallName](const Napi::CallbackInfo &info) {
const QJsonObject effectivePolicy = QtHarmony::mapJsObjectToQJsonObject(info.Env(), info[0]);
showStatusInLog(log, QStringLiteral("%1 succeeded, effective policy: %2").arg(asyncCallName, toCompactJson(effectivePolicy)));
}),
Napi::Function::New(
env,
[log, asyncCallName](const Napi::CallbackInfo &info) {
showFailureInLog(log, asyncCallName, QString::fromStdString(info[0].ToString()));
}),
});
} catch (const Napi::Error &error) {
showFailureInLog(log, asyncCallName, QString::fromUtf8(error.what()));
}
}Ausführen des Beispiels
Wählen Sie „ Key frame mode enabled “ aus und drücken Sie „ Apply “. Passen Sie anschließend die Größe des Fensters durch Ziehen am Rand an und vergleichen Sie dies mit der Größenanpassung im Standardmodus. Das Protokoll zeigt die aktuell gültige Richtlinie an. „ Reset to defaults “ deaktiviert den Key-Frame-Modus.
Das Beispiel erfordert ein Gerät, auf dem der Benutzer die Größe des Fensters durch Ziehen an den Rändern ändern kann. Auf einem Smartphone füllt das Hauptfenster den Bildschirm aus und kann auf diese Weise nicht in der Größe angepasst werden. Auf einem Gerät, das den Keyframe-Modus nicht unterstützt, schlägt der Aufruf 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.