[Solved]Edit qml property from c++



  • I have a qml file that contains a number of properties (it displays a rectangle, and the properties are its dimensions).

    Is it possible for me to be able to alter those properties, lets say the 'width' property, from c++ and have that change reflected on my qml rectangle.


  • Moderators

    Yes.

    @
    object->setProperty("width", 500);
    @

    For more information see "Interacting with QML Objects from C++":http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html .


  • Moderators

    Hi,

    To add to Wieland's answer, if the Item whose property you need to change from C++ is not root Item, you may need to find that particular Item using findChild and passing it the Item's objectName.



  • My qml file looks like this

    @import QtQuick 2.3

    import MyCustoms 1.0

    Rectangle {

    property int    x Val               : 20
    property int    yVal                : 3
    property int    radiusVal        : 10
    
    id: content
    color: "red"
    antialiasing: true
    anchors.fill: parent; anchors.margins: 10; radius: radiusVal
    Rectangle { // smaller rectangle with another colour }
    }@
    

    and I instantiate it from c++ like this

    @view = new QQuickView();

    view->setSource(QUrl("qrc:/qmlRect.qml"));
    view->setResizeMode(QQuickView::SizeRootObjectToView);
    view->show();@

    The first thing I tried was in some function have this

    @view->setProperty("radiusVal", 40);@

    and call it at some point.

    No change whatsoever can be observed on my rectangle though.

    EDIT

    Even if I try

    @QObject *rootObject = view->rootObject();
    rootObject->setProperty("radiusVal",30);@

    won't have any effect.


  • Moderators

    @
    view->setProperty("radiusVal", 40)
    @

    would update QQuickView's property and not Rectangle's.
    The other approach should work. Could be some other problem.



  • It is indeed working, I had another mistake on my source files they prevented the change.



  • I published my third course on Qt this month. The final course is on " Integrating Qt Quick with C++ ":http://bit.ly/qtquickcpp. Watching it might save you some time, your question is covered directly in the course. Pluralsight is a subscription site but they have a free ten-day trial (limited to 4 hours) and I also have some VIP passes left. The VIP pass is good for a week but includes the ability to watch unlimited hours during the week, download the course materials, and watch offline (offline content will expire with the pass). If you want a pass, just send me an email through this forum.



  • Hi Rolias, I will check that out. I don't believe i will need the VIP pass if as you say I can watch 4 hours for free.



  • Of course I hope you'll want to watch more than just one course! Also if you watch with a VIP pass the time you spend watching counts for my course, with the 10 day trial I get no credit.



  • Didn't know that. Well since I plan to watch them anyway I might as well do it with a pass. If you still have any left please do email me.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.