Home > front end >  QML menu style only works in QtWidgets QApplication
QML menu style only works in QtWidgets QApplication

Time:06-09

I am running Qt 5.11.0 under Windows (I understand that it is very old but I cannot upgrade that embedded system).

For some reason, QML Menu from QtQuickControls1 ignores its style if app is a QGuiApplication, but works if it is a QApplication (from QtWidgets).

I also noticed that QApplication uses Desktop style by default, whereas QGuiApplication uses Base style instead and cannot load Desktop style at all.

Is QtWidgets dependency required to load QtQuickControls1 styles correctly, or am I missing something?

Why does QApplication change QML styling at all?

main.cpp

#include <QtQml/qqmlapplicationengine.h>
#include <QtQml/qqmlcomponent.h>
#include <QtWidgets/QApplication>
    
int main(int argc, char ** argv) {
        //QGuiApplication* app = new QApplication(argc, argv); //works
        QGuiApplication* app = new QGuiApplication(argc, argv); //does not work
        QQmlApplicationEngine engine;
        engine.load(QUrl("file:///C:/main.qml"));
        app->exec();
        delete app;
        return 0;
}

main.qml

import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
    
Window {
        visible: true
        Column{
            CheckBox { text: "checkbox"}
            ComboBox { model: ["combobox"] }
            Button { text: "button"}
            Button {
                text: "custom"
                menu: Menu {
                    style: MenuStyle { frame: Rectangle { color: "yellow" } }
                    MenuItem{text: "menuitem"}
                }
            }
        }
}

Expected results:

  • Menu should have yellow background.

Actual results:

qml test results

CodePudding user response:

I tried your code with 5.15.7 and was able to see the yellow rectangle whether I used QApplication or QGuiApplication. So it may be a bug in version 5.11.

  • Related