[英]Update QML Window property flags from C++
I have a basic qml application QWindow that already defines the property flags
.我有一个基本的 qml 应用程序 QWindow,它已经定义了属性
flags
。 I would like to change/update the property flags
from main.cpp
so that it is not frameless.我想更改/更新
main.cpp
中的属性flags
,使其不是无框架的。 I know I can do it directly in QML but I'd like to change it dynamically without re-compiling.我知道我可以直接在 QML 中完成,但我想动态更改它而无需重新编译。 I have found a number of examples but none show how to do this after using
QQmlApplicationEngine::load()
method.我找到了许多示例,但没有一个示例说明在使用
QQmlApplicationEngine::load()
方法后如何执行此操作。 I posted my code below and I am able to see the current flags decimal value 2048
or hex value 0x800
.我在下面发布了我的代码,我能够看到当前标志的十进制值
2048
或十六进制值0x800
。 According to documentation the hex value of 0x800
refers to Qt::FramelessWindowHint
which is the current setting.根据文档,
0x800
的十六进制值指的是Qt::FramelessWindowHint
,这是当前设置。 I have tried to modify the flags
property but the window doesn't update.我试图修改
flags
属性,但 window 没有更新。
// Main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
id: window
objectName: "window"
...
flags: Qt.FramelessWindowHint
...
}
// main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QWindow>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.addImportPath("qrc:/");
QObject::connect(&engine, &QQmlApplicationEngine::quit, &app,
&QGuiApplication::quit);
const QUrl url(QStringLiteral("qrc:/Main.qml"));
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) QCoreApplication::exit(-1);
QVariant obj2 = obj->property("flags");
if (obj2.isValid()) {
Qt::WindowFlags flags = qvariant_cast<Qt::WindowFlags>(obj2);
flags &= ~Qt::FramelessWindowHint;
auto *tmp = dynamic_cast<QWindow*>(obj);
tmp->setFlags(flags);
tmp->show();
}
},
Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
you have obj and if you want that main.qml change you can easily add property to this:你有 obj ,如果你想要那个 main.qml 改变你可以很容易地添加属性到这个:
obj->setProperty("flags", Qt::FramelessWindowHint);
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QWindow>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl)
{
if (!obj && (url == objUrl))
{
QCoreApplication::exit(-1);
}
obj->setProperty("flags", Qt::FramelessWindowHint);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
Edited:编辑:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.