[Interest] QML MenuBar : works with PySide2 but not c++ ?

Nicholas Yue yue.nicholas at gmail.com
Thu Jun 3 03:33:10 CEST 2021


Hi,

  I am learning about QML.

  I would like to find out why the PySide2 loading of the QML file results
in a visible window but the C++ one does not. The compiled application runs
but no window is displayed.

MenuBar.qml
===========
import QtQuick 2.4
import QtQuick.Controls 2.13

ApplicationWindow {
    visible: true
    width: 720
    height: 480
    title: "simple window"

    menuBar: MenuBar{
        Menu{
            title: "Menu1"
        }

        Menu{
            title: "Menu2"
        }

        Menu{
            title: "&Menu3"
        }
    }
}

main.cpp
========
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/MenuBar.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.py
=======
import sys
from PySide2 import QtCore, QtGui, QtQml

if __name__ == '__main__':

    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QtGui.QGuiApplication(sys.argv)

    engine = QtQml.QQmlApplicationEngine()

    url = QtCore.QUrl.fromLocalFile('MenuBar.qml')
    engine.load(url)
    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())

-- 
Nicholas Yue
Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
Custom Dev - C++ porting, OSX, Linux, Windows
http://au.linkedin.com/in/nicholasyue
https://vimeo.com/channels/naiadtools
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210602/ee5fe51a/attachment.html>


More information about the Interest mailing list