0
votes

I have a basic qml application QWindow that already defines the property flags. I would like to change/update the property flags from main.cpp so that it is not frameless. I know I can do it directly in QML but I'd like to change it dynamically without re-compiling. I have found a number of examples but none show how to do this after using QQmlApplicationEngine::load() method. I posted my code below and I am able to see the current flags decimal value 2048 or hex value 0x800. According to documentation the hex value of 0x800 refers to Qt::FramelessWindowHint which is the current setting. I have tried to modify the flags property but the window doesn't update.

// 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();
}
1
Your code actually works fine for me. I'm on Qt 5.15.0 on Linux. - JarMan
why did you create obj2? you can easily setProperty from c++. - Parisa.H.R
@JarMan Hmm. I'm on Qt 5.15.2 MSVC 2019 64 bit. It definitely doesn't work. I even have tried obj->setProperty("flags", 0); to just zero out the flag but doesn't work either. - slayer
Ok, it looks like I have to use obj->setProperty("flags", Qt::Window); when I want to override the Qt.FramelessWindowHint flag. - slayer

1 Answers

0
votes

you have obj and if you want that main.qml change you can easily add property to this:

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();
}

enter image description here

Edited :

enter image description here