Results 1 to 2 of 2

Thread: Access object property in JS

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Access object property in JS

    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:

    Qt Code:
    1. class Container : public QQuickItem {
    2. public:
    3. void checkIntersection() {
    4. foreach (QObject *child, childItems()) {
    5. if (MyObject *obj = dynamic_cast<MyObject*>(child)) {
    6. ...
    7. if(intersected(obj,other_obj)) emit intersect(other_obj);
    8. }
    9. }
    10. }
    11. bool intersected(MyObject * obj,MyObject * other) {
    12. ...
    13. return true;
    14. }
    15. signals:
    16. intersect(MyObject * other);
    17. }
    18.  
    19. class MyObject : public QQuickItem {
    20. ...
    21. }
    To copy to clipboard, switch view to plain text mode 

    and QML structure for better understanding:
    Qt Code:
    1. Container {
    2. MyObject {
    3. id: object1
    4. ...
    5. onIntersect: {
    6. if(other.id === "object2") { // error, id is undefined
    7. doSomething();
    8. }
    9. }
    10. }
    11. MyObject {
    12. id: object2
    13. ...
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access object property in JS

    I think you can do
    Qt Code:
    1. other == object2
    To copy to clipboard, switch view to plain text mode 
    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,
    _

Similar Threads

  1. How to access a nested QML object from C++?
    By TheIndependentAquarius in forum Qt Quick
    Replies: 3
    Last Post: 1st January 2014, 20:12
  2. How do i access the object's properies?
    By ayanda83 in forum Newbie
    Replies: 1
    Last Post: 2nd March 2013, 13:24
  3. Access caller qml-Object from C++ Object
    By xdn in forum Qt Quick
    Replies: 5
    Last Post: 14th February 2013, 10:58
  4. Getting object's property...
    By gilamran in forum Newbie
    Replies: 1
    Last Post: 18th February 2011, 18:07
  5. Access an object from another file.
    By cbarmpar in forum General Programming
    Replies: 1
    Last Post: 6th September 2008, 23:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.