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 you access properties of a QML component in C++?
QtWS25 Last Chance

How do you access properties of a QML component in C++?

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 6 Posters 16.2k Views
  • 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.
  • L Offline
    L Offline
    loladiro
    wrote on last edited by
    #4

    "Link":http://doc.qt.nokia.com/4.7-snapshot/qobject.html#property

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QNewbie
      wrote on last edited by
      #5

      The last post helped quite a bit I am having trouble using it though can't seem to find any examples of people actually using it so not sure how I am supposed to implement that piece of code. In the form it is presented it isn't working for me. Do I need to use Q_PROPERTY macros or anything to make it work? Not really sure if what I need to make it work.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #6

        Yes you need Q_PROPERTY in order for the moc (Meta Object Compiler) to add it to the generated MetaObject, so you can access it via these methods. If you have any further problems, just ask.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thisisbhaskar
          wrote on last edited by
          #7

          @QObject *object = view.rootObject();
          QObject textIn = object->findChild<QObject>(“textIn”);
          textIn->setProperty("text","sampleText"); @

          See if this works.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jupiter
            wrote on last edited by
            #8

            i have a similar problem. i managed to set a value from c++ in my qml file. the value is the x position of a qml rectangle, but when i change the value in c++ the rectangle isnt moved. What am i doing wrong?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #9

              [quote author="Nazgul" date="1308039647"]i have a similar problem. i managed to set a value from c++ in my qml file. the value is the x position of a qml rectangle, but when i change the value in c++ the rectangle isnt moved. What am i doing wrong?[/quote]

              How should we know, if you are not showing us what you are actually doing?

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                QNewbie
                wrote on last edited by
                #10

                I'll check to see if that works but here is something that does work really well

                @
                QDeclarativeProperty property([name of element], "[name of property]");
                @

                and then to call the property just do
                @
                property.read()
                property.write()
                @
                and you can even do stuff like property.read().toString etc. this is what I was really looking for I think but thanks for all the help wouldn't have been able to find it without the leads.

                You still need stuff like
                @
                QObject object = object->findChild<QObject>("rect");
                @
                to use this though

                Edit: use @ tags to markup code sections please.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jupiter
                  wrote on last edited by
                  #11

                  c++ code

                  @QDeclarativeEngine engine;
                  QDeclarativeComponent component(&engine, url);
                  QObject *object = component.create();
                  if (object) {
                  object->setProperty("currentValue", newVal);
                  delete object;
                  object = NULL;
                  }@

                  and my qml file:
                  @Item {
                  id: line
                  objectName: "line"
                  property real currentValue: 100
                  property real minimum: 0
                  property real maximum: 200
                  property int xMax: line.width - rect.width - 4
                  onCurrentValueChanged: { rect.x = 2 + (currentValue - minimum) * line.xMax / (maximum - minimum) }
                  signal valueChanged(real val)
                  width: 400
                  height: 20
                  Rectangle {
                  id: rect
                  x: 0; y: 0
                  width: 100; height: 20
                  color: "red"
                  MouseArea {
                  anchors.fill: parent
                  drag.axis: Drag.XAxis
                  drag.target: rect
                  drag.minimumX: 0
                  drag.maximumX: line.width - rect.width
                  onPositionChanged: {
                  line.currentValue = (maximum - minimum) * (rect.x-2) / line.xMax + minimum
                  line.valueChanged(line.currentValue)
                  }
                  }
                  }
                  }
                  @

                  thanks in advance

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #12

                    What is the delete statement doing on line 6 of your code? What do you expect to happen if you first set a property, and then immediately delete the object?

                    What you are doing here, is creating an object, setting a property on it, and then immediately disposing of it again. That seems like a rather useless thing to do. Instead, you should try to find the object you wish to manipulate, and then modify its properties. Or, perhaps better from a QML/declarative way of thinking kind-of-perspective, you should create a QObject derived object, give it properties for the position, insert it into your QML environment, and bind to these properties.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jupiter
                      wrote on last edited by
                      #13

                      @QObject* obj = mDeclarativeView->rootObject();
                      if (obj) {
                      obj->setProperty("currentValue", newVal);
                      }@

                      ok, i wasnt sure about a memory leak because the method was create.

                      i changed my code to the above and made sure obj != null. obj is a qml object with name = "line"
                      thats fine so far but still the setProperty has no effect.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Jupiter
                        wrote on last edited by
                        #14

                        no ideas about my 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