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

Tony Rietwyk tony at rightsoft.com.au
Thu Jun 3 05:34:03 CEST 2021


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 
> <http://au.linkedin.com/in/nicholasyue>
> https://vimeo.com/channels/naiadtools 
> <https://vimeo.com/channels/naiadtools>
>
> _______________________________________________
> Interest mailing list
> 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/20210603/c2078a38/attachment.html>


More information about the Interest mailing list