0

Item {
    id: valueSource
    objectName: "valueSource"
    property real myvariable: 0
    property real abc: 0
    property real xyz: 0
    SequentialAnimation {
        running: true
        loops: 1

        PauseAnimation {
            duration: 1000
        }

        PropertyAction {
            target: valueSource
            property: "start"
            value: false
        }

        SequentialAnimation {
            loops: Animation.Infinite

            ParallelAnimation {
                NumberAnimation {
                    target: valueSource
                    property: "abc"
                    easing.type: Easing.InOutSine
                    from: a //0
                    to: b //100
                    duration: 3000
                }
                NumberAnimation {
                    target: valueSource
                    property: "xyz"
                    easing.type: Easing.InOutSine
                    from: c //1
                    to: d //50
                    duration: 3000
                }
            }
        }
}

I am using qt and cpp for the first time. So please help me even if it is a silly question. I am able to update value of abc and xyz from c++ code by using findChild("valueSource") but I would like to update values of 'from' and 'to' in NumberAnimation. 1) I tried to add a field objectName in NumberAnimation and then use this object in cpp code as I used to update abc and xyz but not able to modify value of from and to. 2) Also tried using a,b,c,d variables. Values of a,b,c,d are getting updated from cpp code and reflecting in qml as well but it is not updating value of to and from.

Is there any other way I can do it?

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
user21096
  • 11
  • 5

2 Answers2

1

The best way to communicate between c++ and qml is to use signal-slot model. Don't use findChild(). I suggest the following steps for you :

  1. Declare a signal inside your c++ and connect it to a slot, which is in fact a javascript function of your qml rootObject.

  2. Inside this slot (setObjectProperty in my example ) use something like

    function setObjectProperty() { yourObjectId.setProperty( true ); }

where yourObjectId is an id of the Item you'd like to manipulate

  1. Inside your Item declare a setProperty function ( as I named it, you can choose whatever name you like of course =)) )

    function setPoperty ( val ) { internalObjectId.someProperty = val; }

And that should work. If any questions further - let me know, I can provide some code for u.

Dmitry
  • 1,912
  • 2
  • 18
  • 29
0

Thanks for the reply but I got the solution.

Actually, by modifying the to and from value I wanted to achieve smooth transition of a needle because when I was directly updating abc, I was not getting a smooth transition.

But using following method I got smooth transition of needle.

Update abc value from cpp code and add following code in your qml file.

    Behavior on kph{
        NumberAnimation{duration: 2000}
    }
user21096
  • 11
  • 5