Results 1 to 7 of 7

Thread: objectName property doesn't work as expected in C++ defined QML classes

  1. #1
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default objectName property doesn't work as expected in C++ defined QML classes

    I have a custom QML type which I register like this in main.cpp:

    Qt Code:
    1. qmlRegisterType<Gate>("client_gate",1,0,"Gate");
    To copy to clipboard, switch view to plain text mode 

    Then in main.qml I declare it like this:

    Qt Code:
    1. Gate {
    2. id: client_gate
    3. objectName: "oo"
    4. }
    To copy to clipboard, switch view to plain text mode 

    The problem is I can't find it with my C++ code in main.cpp after QML engine is loaded:

    Qt Code:
    1. QObject *root = engine.rootObjects()[0];
    2. o=root->findChild<QObject*>("oo");
    3. if (!o) return 1;
    4. Gate *g;
    5. g=(Gate*) o;
    6. g->set_Client(&client);
    To copy to clipboard, switch view to plain text mode 

    I know the code works, because I can find objects of standard QML type (like SwipeView for example) but it doesn't work with my own QML (C++) classes , findChild() just returns null. Why is this happening?
    Do I have to define "objectName" property in "Gate" c++ class too ? Isn't it supposed that QObject already has "objectName" property ?

    Any help will be greately appreciated.
    TIA

  2. #2
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default Re: objectName property doesn't work as expected in C++ defined QML classes

    Quote Originally Posted by nuliknol View Post
    Why is this happening?
    I commented out all the QML objects except my custom C++ class called "Gate" and ran this code:
    Qt Code:
    1. QList<QObject *> list = root -> findChildren<QObject *> ();
    2.  
    3. qWarning() << "list size is " << list.size();
    4. for(int i=0;i<list.size();i++) {
    5. qWarning() << "i="<<i<<", "<< list.at(i) << " name="<<list.at(i)->objectName();
    6. }
    To copy to clipboard, switch view to plain text mode 

    and I have got this output:
    Qt Code:
    1. list size is 0
    To copy to clipboard, switch view to plain text mode 

    So aparently my custom C++ object is not included in the children's list, but I do have it in main.qml:

    Qt Code:
    1. Gate {
    2. objectName: "oo"
    3. id: client_gate
    4. }
    To copy to clipboard, switch view to plain text mode 

    Very weird stuff.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: objectName property doesn't work as expected in C++ defined QML classes

    Is your "Gate" element the top element itself? I.e. it is "root"?

    In any case, you probably don't need that at all, it is very uncommon to access QML instantiated objects like that.

    Cheers,
    _

  4. #4
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default Re: objectName property doesn't work as expected in C++ defined QML classes

    Quote Originally Posted by anda_skoa View Post
    Is your "Gate" element the top element itself? I.e. it is "root"?

    In any case, you probably don't need that at all, it is very uncommon to access QML instantiated objects like that.

    Cheers,
    _
    No, it is not root, it is a child of the ApplicationWindow. I wonder why it is uncommon. If you have some objects in main.cpp how do you link objects in QML to those that are located in main.cpp at application startup from main.cpp itself ?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: objectName property doesn't work as expected in C++ defined QML classes

    Quote Originally Posted by nuliknol View Post
    No, it is not root, it is a child of the ApplicationWindow.
    Hmm. Have you tried with something else than a ApplicationWindow?
    Or having the Gate inside an Item in the window?

    Quote Originally Posted by nuliknol View Post
    I wonder why it is uncommon.
    Because you usually will want to avoid a C++ -> QML dependency. i.e. making the more static C++ side depend on the more dynamic QML side.
    This kind of dependency restricts what you can do QML side.

    Quote Originally Posted by nuliknol View Post
    If you have some objects in main.cpp how do you link objects in QML to those that are located in main.cpp at application startup from main.cpp itself ?
    By exposing C++ API to QML instead.

    Cheers,
    _

  6. #6
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default Re: objectName property doesn't work as expected in C++ defined QML classes

    I think I found the answer to why the object was not found by root->findChild<QObject*> call. I think it wasn't found because my type (I have registered with qmlRegisterType) is not an Item-derived object.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: objectName property doesn't work as expected in C++ defined QML classes

    Quote Originally Posted by nuliknol View Post
    I think I found the answer to why the object was not found by root->findChild<QObject*> call. I think it wasn't found because my type (I have registered with qmlRegisterType) is not an Item-derived object.
    Right, objects instantiated in QML might not have the QObject parent one thinks they have.
    Another very good reason not to try findChild() or findChildren() on a QML object tree in the first place.

    Aside from being an unnecessary hack it is also an unreliable hack.

    Cheers,
    _

Similar Threads

  1. setFixedSize doesn't work as expected
    By jeremejevs in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2013, 08:50
  2. Replies: 2
    Last Post: 6th August 2010, 09:10
  3. QDataStream doesn't work as expected
    By mastupristi in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 07:08
  4. Replies: 9
    Last Post: 2nd December 2009, 18:59
  5. QHTTP::GET doesn't work as expected !
    By tuthmosis in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2008, 15:45

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.