June 9, 2011

QNewbie QNewbie
Lab Rat
4 posts

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

 

Okay so I understand you can connect signals and slots between QML and C++ objects and how to pass stuff generated in C++ to QML but how would I go about calling property of QML object for use in C++. Basically what I am trying to do is take text input from the TextInput component in QML and use it to set the currentPath() of an object which in the QDir class. I am sure I messed up a lot syntax and stuff there but I am pretty new to this.

I was thinking of doing something like

  1. QObject *object = view.rootObject();
  2. QObject *textIn = object->findChild<QObject*>("textIn");

and then calling text property like so and using QString to make a string.
  1. QString textholdervariable(textIn.text());

and then doing the following to set my QDir path
  1. QDir dir;
  2. QDir::setCurret(textholderviable);  

But this doesn’t work as I get following error

request for member ‘text’ in ‘textIn’, which is of non-class type ‘QObject*’

Anyone know the proper way to go about retrieving properties of QML objects for use in C++?
I would also be okay with being able to set the a variable in QML equal to some variable and then being able to call that in C++.
I would also be okay with being told I am asking a stupid question and what I am talking about is impossible or there is a better way to do such a thing any help would be great.

Edit: use @ tags to markup code sections please; Andre

13 replies

June 9, 2011

situ117 situ117
Lab Rat
42 posts

Look at:
Locating child objects in QML [doc.qt.nokia.com]

June 9, 2011

QNewbie QNewbie
Lab Rat
4 posts

I have seen that but isn’t that for changing the properties in QML? I am trying to take properties from the QML object and use them as inputs for C++ functions.

June 10, 2011

loladiro loladiro
Lab Rat
596 posts

Link [doc.qt.nokia.com]

June 13, 2011

QNewbie QNewbie
Lab Rat
4 posts

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.

June 13, 2011

loladiro loladiro
Lab Rat
596 posts

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.

June 14, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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

See if this works.

June 14, 2011

Jupiter Jupiter
Lab Rat
95 posts

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?

June 14, 2011

Andre Andre
Robot Herder
6420 posts
Nazgul wrote:
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?

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

June 14, 2011

QNewbie QNewbie
Lab Rat
4 posts

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

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

and then to call the property just do

  1. property.read()
  2. 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

  1. QObject *object = object->findChild<QObject*>("rect");

to use this though

Edit: use @ tags to markup code sections please.

June 14, 2011

Jupiter Jupiter
Lab Rat
95 posts

c++ code

  1. QDeclarativeEngine engine;
  2.    QDeclarativeComponent component(&engine, url);
  3.    QObject *object = component.create();
  4.    if (object) {
  5.     object->setProperty("currentValue", newVal);
  6.     delete object;
  7.     object = NULL;
  8.    }

and my qml file:

  1. Item {
  2.  id: line
  3.  objectName: "line"
  4.  property real currentValue: 100
  5.  property real minimum: 0
  6.  property real maximum: 200
  7.  property int xMax: line.width - rect.width - 4
  8.  onCurrentValueChanged: { rect.x = 2 + (currentValue - minimum) * line.xMax / (maximum - minimum) }
  9.  signal valueChanged(real val)
  10.  width: 400
  11.  height: 20
  12.  Rectangle {
  13.   id: rect
  14.   x: 0; y: 0
  15.   width: 100; height: 20
  16.   color: "red"
  17.   MouseArea {
  18.    anchors.fill: parent
  19.    drag.axis: Drag.XAxis
  20.    drag.target: rect
  21.    drag.minimumX: 0
  22.    drag.maximumX: line.width - rect.width
  23.    onPositionChanged: {
  24.     line.currentValue = (maximum - minimum) * (rect.x-2) / line.xMax + minimum
  25.     line.valueChanged(line.currentValue)
  26.    }
  27.   }
  28.  }
  29. }

thanks in advance

June 14, 2011

Andre Andre
Robot Herder
6420 posts

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.

June 14, 2011

Jupiter Jupiter
Lab Rat
95 posts

  1. QObject* obj = mDeclarativeView->rootObject();
  2.    if (obj) {
  3.     obj->setProperty("currentValue", newVal);
  4.    }

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.

June 15, 2011

Jupiter Jupiter
Lab Rat
95 posts

no ideas about my problem?

 
  ‹‹ [Solved] Scroll bar visible when scrolling      [sloved]convert int to string in qml? ››

You must log in to post a reply. Not a member yet? Register here!