Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I register C++ defined types using QQmlApplicationEngine? And should I even be using QQmlApplicationEngine?



  • I'm trying to learn more about QtQuick, specifically, I want to register my C++ defined class with the QML engine. I'm using QGuiApplication and QQmlApplicationEngine (as setup automatically by QtCreator for a QtQuick2 project), but I'm not sure how or where to register my new type? In tutorials that I've found, typically they're calling the qmlRegisterType function, which doesn't seem to be a member function of QQmlApplicationEngine. This is odd to me, because the documentation has it listed as a member of the QQmlApplicationEngine's parent class (QQmlEngine). So how do I do it? And why does it seem like QQmlApplicationEngine is missing some functionality of QQmlEngine and QQmlComponent?

    As a related follow-up, should I even be using QQmlApplicationEngine, or is that a 'simpler' method designed for smaller projects? I've noticed that QQmlApplicationEngine is kind of a combination of QQmlEngine and QQmlComponent classes.. So when is it better to use those two discrete classes instead of the combination class? It seems like there are a lot of different ways to do the same thing (not to mention things like QQuickView, which is also confusing to me..)

    If anyone can help me figure out all these different routes to QtQuick initialization, it'd be much appreciated!



  • You just call the qmlRegisterType in the main. This function is a non-member function defined in the QQmlEngine header so you don't call it with a QQmlEngine object.



  • Hi,

    Here a link to an example i use in my trainings

    https://www.dropbox.com/sh/mrrb5cm2kq1h44s/qLWmrPL7Pw



  • Quick example. It's just my main (shrinked of useless declarations):

    @
    Application app(argc, argv);

    // Viewer object to handle QML types
    QtQuick2ControlsApplicationViewer viewer;

    // Registering CUSTOM types
    qmlRegisterType<ClassName>("ClassName", 1, 0, "QMLNAME");

    // Registering SINGLETON CUSTOM types
    qmlRegisterSingletonType<AppInfo>("qt.appInfo", 1, 0, "AppInfo", singletonObject);

    // Load initial QML file
    viewer.setMainQmlFile(QStringLiteral("qml/HS2/main.qml"));
    viewer.show();

    // main loop starts
    return app.exec();
    @

    In this example I've registered both a singleton class (i.e. a class which is instanced only ONCE in the program) and a non-singleton one. In simple projects you can always set the registration URL (first parameter) equal to the name of the class. Here I've added "qt" as the initial url. In the QML I can import the type and create an object as usual:

    @
    import ClassName 1.0 // <--- Major/minor == values passed to registration!

    QMLNAME {
    id: myId
    width: ...
    height: ...
    anchors.centerIn: parent
    Component.onCompleted: initFunctionDefinedInTheRegisteredTypeIfAny()
    }
    @

    Have a look to "this page":http://qt-project.org/doc/qt-5/qtqml-cppintegration-definetypes.html for further examples and clarifications! Enjoy! ;)


Log in to reply