Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How do I register C++ defined types using QQmlApplicationEngine? And should I even be using QQmlApplicationEngine?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 4 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    katanaswordfish
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stevenceuppens
        wrote on last edited by
        #3

        Hi,

        Here a link to an example i use in my trainings

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

        Steven CEUPPENS
        Developer / Architect
        Mobile: +32 479 65 93 10

        1 Reply Last reply
        1
        • D Offline
          D Offline
          deleted529
          wrote on last edited by
          #4

          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! ;)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved