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

Nicholas Yue yue.nicholas at gmail.com
Thu Jun 3 06:08:42 CEST 2021


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> 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 listInterest at qt-project.orghttps://lists.qt-project.org/listinfo/interest
>
> _______________________________________________
> Interest mailing list
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210602/5fcd3128/attachment.html>


More information about the Interest mailing list