PDA

View Full Version : Access object property in JS



folibis
18th January 2014, 05:23
I've created a couple of my own objects, container and item
Items are movable and periodically I check for some condition, let's say fot objects intersection.
If required event occurs, the object emits a signal:


class Container : public QQuickItem {
public:
void checkIntersection() {
foreach (QObject *child, childItems()) {
if (MyObject *obj = dynamic_cast<MyObject*>(child)) {
...
if(intersected(obj,other_obj)) emit intersect(other_obj);
}
}
}
bool intersected(MyObject * obj,MyObject * other) {
...
return true;
}
signals:
intersect(MyObject * other);
}

class MyObject : public QQuickItem {
...
}

and QML structure for better understanding:

Container {
MyObject {
id: object1
...
onIntersect: {
if(other.id === "object2") { // error, id is undefined
doSomething();
}
}
}
MyObject {
id: object2
...
}
}

As you can see, when I trying to get object property (id) I get error. I can read some properties, width or height for example, but id is inaccesible.
What I do wrong?

anda_skoa
18th January 2014, 13:23
I think you can do


other == object2

i.e. using the id like a reference to the object

Another option would be to set the "objectName" property and compare with that.

Cheers,
_