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. QmlRegisterType and setContextProperty
Qt 6.11 is out! See what's new in the release blog

QmlRegisterType and setContextProperty

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 9.1k 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.
  • M Offline
    M Offline
    Mabrouk
    wrote on last edited by
    #1

    i just wanna know when it's best to use which ?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Where you think you need it :)

      Registering a type makes it possible for you to add new QML "classes" (objects) available throughout you app. You have to instantiate them somewhere in QML code. This way you can implement some custom functionality, painting etc. in C++ and have it available in QML - in every instance you create.

      Context property exposes an already instantiated property to the QML side (a single property. You can also set context object to get e.g. new global functions). You can use that for a lot of things, but not as those "classes" mentioned above. For example, in one of my apps, I have set a "PWD" property in C++, and used it in QML to get a common base URL for relative paths.

      So, it really depends on what you want to achieve.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mabrouk
        wrote on last edited by
        #3

        i get it now , thanks sierdzio :)

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          pleasure.

          I was trying to keep it short, but if there is anything still not clear, ask. Or, even better - try in some test app.

          (Z(:^

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mabrouk
            wrote on last edited by
            #5

            well i have another question which may not be so related
            if i want to attach a certain object or property with certain qml component, for example a huge list model to be only available when an instance of the component is there, how can i do that

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              The usual approach on a forum is to start a new thread for every new topic. But never mind that for now, the second question is more or less related. You can request moderators to do a split, if you want to.

              Back to the question. 2 ways that immediately spring to my mind are:

              • create a casual QML file with ale the "scaffolding" for this list model (GridView, Delegate, empty (!) ListModel). Then, add a JS method (or provide C++ Invokable method) that would populate the model on the fly - when invoked (ListModel::append() is the method you need to use there), or even automatically on creation (Component.onCompleted)
              • subclass your component of choice in C++, add handling to this big data model, and add this to QML with qmlRegisterType()

              (Z(:^

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mabrouk
                wrote on last edited by
                #7

                well i understand it pretty well theoretically but don't know how it can be done in coding, if you can provide me with a little example that would be great :)
                and thanks alot :)

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  For the first option, an example can be found here (my second comment in that topic): "link":http://developer.qt.nokia.com/forums/viewthread/13042/

                  For the second one, I do not have any, you'll have to devise that yourself. A helper code may be my QWebService project, and it's QML bindings ("link":https://gitorious.org/qwebservice), but it would take you some time to look through that code. I suggest reading Qt docs and trying on your own.

                  (Z(:^

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mabrouk
                    wrote on last edited by
                    #9

                    i will do my best, thanks sierdzio :)

                    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