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. Expose variable to QML AND C++
Forum Updated to NodeBB v4.3 + New Features

Expose variable to QML AND C++

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

    I want to expose a variable declared in the main procedure to QML.
    I do this with the following
    @
    XmlType xmlFile;
    viewer.rootContext()->setContextProperty("xmlFile", &xmlFile);
    @

    • declaration of type in xmltype.cpp/.h
    • declaration of variable in main.cpp
    • presentation of variable in show.qml
    • want to alter the data in change.cpp

    But how to access this variable from another class?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      Hello,

      if you want to access the property of XML type, you had to use the Properties like :
      @Q_PROPERTY(QString dateHeure READ getDateHeure WRITE setDateHeure NOTIFY dateHeureChanged)@

      and of course implements the functions, then you can use them in your QML file with the syntax xmlFile.dateHeure, and modify the values as well.

      A special syntax is used for lists (see declarativeListProperty),
      or you can use models....

      dmcr

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rocken
        wrote on last edited by
        #3

        Thank you for your code snippet

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rocken
          wrote on last edited by
          #4

          Can you explain how to make a list available in QML.
          You described that I can use the qdeclarativeListProperty or a model.

          In my xmlFile-Object exists a List which you get when you call the function getParameters within.
          The return type is a ListModel, as described in "here":http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html

          I thought on something like this in QML:

          @
          ListView {
          model: xmlFile.getParamters()
          delegate:
          Item {
          Text {
          text: xmlFile.getParameterName
          }
          }
          }
          @

          The list holds Parameter-Objects (in the link above these where fruititem-Objects) which have several fields like name (getParameterName) or value.
          I can't adapt the example from the link because I didn't define my List in the main.cpp but in the xmlFile-Object.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dmcr
            wrote on last edited by
            #5

            Hi,

            in the QML you don't have to use the function, but the name used in the Q_PROPERTYdeclaration :

            xml.parameter, or xmlFile.parameterName, in the example
            @names[NameRole] = "name";@ the parameterName is "name"

            However the example is a good one!

            you could add the @ view.rootContext()->setContextProperty("fruitsModel", createModel());@
            where you want even if it's not in the main, as soon as you acess to the declarativeView, it will also works

            dmcr

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rocken
              wrote on last edited by
              #6

              OK, it is clear to me that I have do use the defined names in the Q_PROPERTY.

              How to set the Q_PROPERTY in the main.cpp for the list?

              The list comes from the Function: xmlFile.getParameters() and holds Parameter-Object.
              The delegates in the ListView shoud display name and value of each object in the list.
              Further the QML-File do not know about the Parameter-Type.

              Is this the result:
              @
              Q_PROPERTY(QString parameter READ xmlFile.Parameter() )
              Q_PROPERTY(QString name READ xmlFile.Parameter().name())
              @

              Or can I do something like that:

              @
              ctxt->setContextProperty("parameters", xmlFile.getParameters());
              ctxt->setContextProperty("name", xmlFile.getParameters().name());
              @

              I think I also should expose the Parameter-Type. Is this right?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rocken
                wrote on last edited by
                #7

                I got it working with the following lines:

                main.cpp
                @
                ctxt->setContextProperty("parameters", xmlFile.getParameters());
                @

                QML
                @
                ListView {
                model: parameters
                delegate:
                Item {
                Text {
                text: name
                }
                }
                }
                @

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rocken
                  wrote on last edited by
                  #8

                  Now I can add Elements with appendRow().
                  But when I want to delete all the list entries with clear() strange things happen:

                  I do the following:

                  addRow() x 6

                  clear()

                  addRow() x 4

                  Now I have 6 elements in the list:
                  elements 1 to 4 are from 3. , elements 5 to 6 are from 1.
                  How can this be?

                  If I first add 4 elements, clear, and then add 6 elements I get 7 elements in my ListView:
                  elements 1 to 6 are from the addRow() after the clear and element 7 is from the first addRow() before the clear().

                  I hope this is not too confusing.

                  Can it be, that there is some kind of memory leak due to the setContextProperty() thing?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rocken
                    wrote on last edited by
                    #9

                    I found out that flicking/scrolling the ListView erases the wrong items.
                    This doesn't solve my problem but maybe helps unterstanding it.
                    The rowCount() also gives the correct number of items. Only the view is not correct.

                    Is there someone with the same problem?

                    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