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

Jérôme Godbout godboutj at amotus.ca
Fri Jun 4 16:59:43 CEST 2021


The resources usage seem to be right (added to .pro properly and the name and path seem to match) and the case are correct (assuming the actual file has the same cases). The Qml module must be added like stated below but you might want to also add core and quick control2:
QT += core qml quick quickcontrols2

You might want to add a trace before exiting -1 into your main.cpp:
QDebug() << “Failed to load the main qml script”;
QCoreApplication::exit(-1);

Also check the application output for any error or the Qml output into Qt creator (if you use it) it might tell you it miss some plugins or cannot find something. Maybe you Qml as an error or cannot find a component, it will simply not load making the application return -1 above.



From: Interest <interest-bounces at qt-project.org> on behalf of Frank Mertens <frank at cyblogic.de>
Date: Friday, June 4, 2021 at 6:02 AM
To: interest at qt-project.org <interest at qt-project.org>
Subject: Re: [Interest] QML MenuBar : works with PySide2 but not c++ ?
No, only QML components need to be uppercase. The C++ and Python code looks 100% the same. The only difference is that the C++ code uses qrc. Must be a bug!-) This is Qt 5.15.

On 03.06.21 15:19, Jason H wrote:
Why? QML elements are capitalized. They must be.
the only exception is js imports: import "app.js" as App



Sent: Thursday, June 03, 2021 at 4:10 AM
From: "Frank Mertens" <frank at cyblogic.de><mailto:frank at cyblogic.de>
To: interest at qt-project.org<mailto:interest at qt-project.org>
Subject: Re: [Interest] QML MenuBar : works with PySide2 but not c++ ?
Try a lowercase file name, e.g.: "qrc:/menuBar.qml".

;)


On 03.06.21 06:08, Nicholas Yue wrote:
I copied the qml loading code from another working example, I was just testing a change in the QML content.

I'd have to dig further about the lambda

MenuBar.pro
===========
QT += quick

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

RESOURCES += main.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.qrc
=======
<RCC>
    <qresource prefix="/">
        <file>MenuBar.qml</file>
    </qresource>
</RCC>


On Wed, 2 Jun 2021 at 20:36, Tony Rietwyk <tony at rightsoft.com.au<mailto:tony at rightsoft.com.au>> wrote:

Hi Nicholas,

The short answer is because your C++ is doing completely different things to the python code.  :O)

I'm not sure about using QML.  Have you included the qml file as a resource correctly to access via qrc:?  You aren't checking the result of the engine.load.  Also, why is the lambda exiting the application when the objectCreated matches the url?

Have run the Qt QML examples?  How is your C++ code different to those?

Hope that helps, Tony


On 3/06/2021 11:33 am, Nicholas Yue wrote:
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


_______________________________________________

Interest mailing list

Interest at qt-project.org<mailto:Interest at qt-project.org>

https://lists.qt-project.org/listinfo/interest
_______________________________________________
Interest mailing list
Interest at qt-project.org<mailto:Interest at qt-project.org>
https://lists.qt-project.org/listinfo/interest


--
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


_______________________________________________

Interest mailing list

Interest at qt-project.org<mailto:Interest at qt-project.org>

https://lists.qt-project.org/listinfo/interest

_______________________________________________ Interest mailing list Interest at qt-project.org<mailto:Interest at qt-project.org> https://lists.qt-project.org/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210604/92f331cb/attachment.html>


More information about the Interest mailing list