Results 1 to 4 of 4

Thread: How to access a nested QML object from C++?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to access a nested QML object from C++?

    Here is a reproducible example:

    main.qml

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item {
    4. id : root
    5. width: 360
    6. height: 360
    7.  
    8. Text {
    9. id : t1
    10. text: qsTr("Hello World")
    11. property int someNumber: 1000
    12. anchors.centerIn: parent
    13. }
    14. MouseArea {
    15. anchors.fill: parent
    16. onClicked: {
    17. Qt.quit();
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QtGui/QGuiApplication>
    2. #include <QQmlEngine>
    3. #include <QQmlComponent>
    4. #include <QQmlProperty>
    5. #include <QDebug>
    6.  
    7. #include "qtquick2applicationviewer.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QGuiApplication app(argc, argv);
    12.  
    13. QtQuick2ApplicationViewer viewer;
    14. viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml"));
    15. viewer.showExpanded();
    16.  
    17. QQmlEngine engine;
    18. QQmlComponent component(&engine, "qml/untitled/main.qml");
    19. QObject *object = component.create();
    20.  
    21. qDebug() << "Property value:" << QQmlProperty::read(object, "root.ti.someNumber").toInt();
    22.  
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 


    I wish to access the property somenumber of the text of the QML Item. The above method isn't producing the desired result.

    How to do it?

  2. #2
    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: How to access a nested QML object from C++?

    QQmlProperty::read() needs the object from which to read the property. So you need to find the Text element.
    You can set the objectName property on the Text element and then use QObject::findChild on the object you have.

    However, I would strongly advise to reconsider doing that at all. Accessing QML generated objects from C++ like that tightly couples the C++ code to the QML code, which is usually the very opposite of what one would want.

    What is the goal you are trying to reach?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access a nested QML object from C++?

    However, I would strongly advise to reconsider doing that at all. Accessing QML generated objects from C++ like that tightly couples the C++ code to the QML code, which is usually the very opposite of what one would want.
    Agreed. I think the preferred way to do this is the same way you would make a C++ class - hide the implementation details behind public methods that do what you need behind the scenes. Users of the public UI don't need to know anything about what happens inside the class.

    In the QML case, you can declare functions inside your top-level Item that reach down into the QML hierarchy to get, set, or do something. Outside the QML, the user only knows that there is some function that can retrieve a number from somewhere. This enables you to loosely couple the C++ and QML. Another alternative is to use signals and slots to talk between the two layers.

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item {
    4. id : root
    5. width: 360
    6. height: 360
    7.  
    8. function getSomeNumber() {
    9. return t1.someNumber
    10. }
    11.  
    12. function setSomeNumber( int number ) {
    13. t1.someNumber = number
    14. }
    15.  
    16. Text {
    17. id : t1
    18. text: qsTr("Hello World")
    19. property int someNumber: 1000
    20. anchors.centerIn: parent
    21. }
    22. MouseArea {
    23. anchors.fill: parent
    24. onClicked: {
    25. Qt.quit();
    26. }
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 1st January 2014 at 18:25.

  4. #4
    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: How to access a nested QML object from C++?

    Quote Originally Posted by d_stranz View Post
    In the QML case, you can declare functions inside your top-level Item that reach down into the QML hierarchy to get, set, or do something. Outside the QML, the user only knows that there is some function that can retrieve a number from somewhere. This enables you to loosely couple the C++ and QML. Another alternative is to use signals and slots to talk between the two layers.
    Or a propery on the C++ side that you simply bind to in QML.

    Cheers,
    _

Similar Threads

  1. How do i access the object's properies?
    By ayanda83 in forum Newbie
    Replies: 1
    Last Post: 2nd March 2013, 12:24
  2. Access caller qml-Object from C++ Object
    By xdn in forum Qt Quick
    Replies: 5
    Last Post: 14th February 2013, 09:58
  3. Meta object features not supported for nested classes
    By ustulation in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2012, 16:36
  4. Replies: 12
    Last Post: 26th June 2011, 11:26
  5. Access an object from another file.
    By cbarmpar in forum General Programming
    Replies: 1
    Last Post: 6th September 2008, 22:17

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.