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. What is the best way to change attributes in QML from C++
Forum Updated to NodeBB v4.3 + New Features

What is the best way to change attributes in QML from C++

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.2k 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.
  • Q Offline
    Q Offline
    qtd1d1
    wrote on last edited by
    #1

    Hi,

    what is the best way to change attributes in QML from C++? I know that there are different solutions to this problem.

    This are the solutions I know:
    1. Solution
    Set a context property in c++ (e.g. Rotation) and read it in QML.
    C++:
    @this->rootContext()->setContextProperty("var", var);@
    QML:
    @rotation: var@

    2. Solution
    Emmit a signal in c++ and connect it with a slot in QML.
    C++:
    @emit started(var);@
    QML:
    @onStarted: rotation(value)@

    3. Solution
    Use a javascript function inside your QML and invoke it from C++.
    To be honest I don't like this approach. QML should cover only the UI and C++ the logic.
    Writing a javascript function into your QML file, adds logic to the UI. -> maintanence and bug fixing gets more difficult
    That's why I won't use this approach.

    So, any suggestions (regarding performance or improvment) about the different ways to handle this problem?
    Or do you even know any other way to change attributes from QML?
    Thanks

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

      This doc mentions a few additional approaches: "LINK":http://developer.qt.nokia.com/doc/qt-4.7/qtbinding.html

      (Z(:^

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

        And about your 3rd solution: don't dismiss it that fast. JS capabilities in QML are quite useful, and there may be use cases, where this would be favourable.

        In an app I'm currently writing, I am using JS quite a lot, because it's simply easier to write some functions in it (especially ones that reference a lot of other QML objects visible in a file), than going back to C++ and throwing in a lot of qobject_casts etc. I might transfer some functions to C++ later on, but still, a lot will probably remain where they are.

        (Z(:^

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qtd1d1
          wrote on last edited by
          #4

          Thanks for the quick response.
          I would like to know if there is a performance difference between these approaches.

          Regarding the link you sent me:
          I had given it a short look already, but it doesn't cover the performance questions.
          Another thing would be that I would like to send a signal from c++ to a rectangle in QML (not an own object).
          Any ideas how I could do that?

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

            I have not benchmarked those, nor have I heard about any benchmark done anywhere. But, all those approaches make use of Qt's Meta Object System, so most probably performance of all of them is quite similar (nonetheless, check for yourself to be sure, that's just a guess).

            (Z(:^

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qtd1d1
              wrote on last edited by
              #6

              Ok thank you.

              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