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. Connection C++ QML [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Connection C++ QML [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 2.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.
  • J Offline
    J Offline
    jarosda
    wrote on 26 Nov 2013, 06:35 last edited by
    #1

    Hi, could someone please recommend me the best way how to interconnect c++ with QML. I will write an application for Android which will work as TCP server. I'd like to use QML2 for GUI requirements. But I am little bit lost at this moment. The base project should be created as C++ application with connected QML files or vice versa? It would be nice if someone will have links or advice. Thank you in advance.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 26 Nov 2013, 06:56 last edited by
      #2

      Hi,

      Follow "this":http://qt-project.org/forums/viewthread/33749/#146966 for a simple C++ - QML integration example.
      Also
      "qtqml-cppintegration-topic":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-topic.html , "qtqml-cppintegration-interactqmlfromcpp":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-interactqmlfromcpp.html and "qtqml-cppintegration-exposecppattributes":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html explains all the required stuff.

      I always select "Qt Quick 2 Application (Built-in Types)" from Qt Creator as base project for QML based applications as it creates all the necessary stuff.

      Hope this helps..

      157

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jarosda
        wrote on 26 Nov 2013, 07:08 last edited by
        #3

        Ok, thank you, it seems to be enough for me at this moment. Thanks again.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jarosda
          wrote on 26 Nov 2013, 09:13 last edited by
          #4

          After reading I have still one question. What is better way.... accessing QML from C++ application or C++ parts from QML application? I'd like to make nice GUI for Android but still I need have a control on business logic in c++ too.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 26 Nov 2013, 09:28 last edited by
            #5

            IMHO, i too don't know the advantages of one over the other. But i consider accessing C++ classes from QML application. It's more easier to understand. In both cases the business logic is implemented in C++ classes.

            157

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jarosda
              wrote on 26 Nov 2013, 09:57 last edited by
              #6

              Ok, however thank you for input. Maybe somebody else will replay...

              1 Reply Last reply
              0
              • U Offline
                U Offline
                utcenter
                wrote on 26 Nov 2013, 10:23 last edited by
                #7

                Short summary:
                Use properties
                Use signals and slots
                Use Q_INVOKABLE and Q_ENUMS

                This is really all you need to expose your C++ logic to QML. You can either inherit QObject if you are writing new code, or you can just wrap your existing "legacy" classes in a QObject and provide an interface with properties, signals and slots.

                There was a good talk on the subject on the last devdays, while the videos are yet not available, you may still find this "PDF presentation":http://www.qtdeveloperdays.com/sites/default/files/presentation_pdf/QtDevDaysSFO-2013_WrappingCppForQml_final.pdf to be useful.

                You should not access QML from C++, your C++ logic should be completely agnostic of the UI. Your C++ code should not even concern with the existence of QML, your only concern should be exposing what you need to access from C++ through the Qt meta system, which is what both C++ and QML can use directly out of the box. You can wrap individual classes and use them as building blocks or you can wrap a large scale logic, sort of a "solidified" logic core that you may instance once and use it to control your entire application. You even have the option of instantiating your wrapped objects in QML or in C++ and just make them available in the QML context.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jarosda
                  wrote on 26 Nov 2013, 10:38 last edited by
                  #8

                  Thank you, I understood QML exploits C++ logic and C++ doesn't rely on QML GUI. This thread could be solved for me.

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    utcenter
                    wrote on 26 Nov 2013, 11:19 last edited by
                    #9

                    Note that it is still possible to access and control QML from C++, again through the meta system, but consider this to be "last resort" and most certainly not recommended practice.

                    QML will handle all data pretty well, provided of course you have registered those type to the meta system. I'd say it is even safe to use raw pointers through QML with properties and methods - something most people will probably advise against for being unsafe, but hey, pointers are unsafe in C++ too :) I actually work on a project that passes around C++ pointers between components instantiated in QML and it works flawlessly.

                    1 Reply Last reply
                    0

                    9/9

                    26 Nov 2013, 11:19

                    • Login

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