Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]Edit qml property from c++
Forum Updated to NodeBB v4.3 + New Features

[Solved]Edit qml property from c++

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 4 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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 .

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        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.

        157

        1 Reply Last reply
        0
        • ealioneE Offline
          ealioneE Offline
          ealione
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

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

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

            157

            1 Reply Last reply
            0
            • ealioneE Offline
              ealioneE Offline
              ealione
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rolias
                wrote on last edited by
                #7

                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.

                Check out my third course in the trilogy on Qt
                "Integrating Qt Quick with C++"
                http://bit.ly/qtquickcpp
                published by Pluralsight

                1 Reply Last reply
                0
                • ealioneE Offline
                  ealioneE Offline
                  ealione
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rolias
                    wrote on last edited by
                    #9

                    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.

                    Check out my third course in the trilogy on Qt
                    "Integrating Qt Quick with C++"
                    http://bit.ly/qtquickcpp
                    published by Pluralsight

                    1 Reply Last reply
                    0
                    • ealioneE Offline
                      ealioneE Offline
                      ealione
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved