[Interest] How to set QtQuickControls Slider to an initial value without modifying the current saved?

Bo Thorsen bo at vikingsoft.eu
Fri Oct 17 10:26:13 CEST 2014


Well, yes and no. Often, when you rely on the onCompleted, this points 
to a flaw in your code or design. (I said often, guys, flames not needed 
:) Sometimes these checks are necessary.)

However, in your specific case, you could actually solve this without 
problems:

value: settings.trackWidth
onValueChanged: {
     if (value != settings.trackWidth) {
         settings.trackWidth = value
     }
}

Bo.

Den 17-10-2014 10:19, Nuno Santos skrev:
> Thanks Bo for your reply.
>
> Don’t you guys think that is kind of strange there isn’t a right built 
> in support for this situation? :/
>
> Regards,
>
> Nuno
>
> On 17 Oct 2014, at 07:49, Bo Thorsen <bo at vikingsoft.eu 
> <mailto:bo at vikingsoft.eu>> wrote:
>
>> Den 16-10-2014 17:39, Nuno Santos skrev:
>>> Hi,
>>>
>>> I’m using Slider from QtQuickControls and i’m facing a problem. I 
>>> initialise the value with a value saved on settings. However, when 
>>> the slider is instantiated, it is triggering onValueChanged with the 
>>> default min value. The setting is being correctly saved because I 
>>> don’t show the slider right away. Only when the slider is show, this 
>>> is happening and I see the result of the change in settings.
>>>
>>> Slider {
>>>     width: parent.width
>>>     height: 10
>>>     minimumValue: 60
>>>     maximumValue: 180
>>>     stepSize: 1
>>>     updateValueWhileDragging: true
>>>     value: settings.trackWidth
>>>     onValueChanged: {
>>>         console.log("track width value changed: " + value)
>>>         settings.trackWidth = value
>>>     }
>>>     Component.onCompleted: {
>>>         console.log("track width on completed: " + settings.trackWidth)
>>>         value = settings.trackWidth
>>>     }
>>> }
>>>
>>> Console:
>>>
>>> qml: track width value changed: 60
>>> qml: track width on completed: 60
>>
>> It's possible there is an elegant solution to this, but I haven't found
>> it so far. What I do in cases like this is to set a boolean property
>> initialised to false and set this to true in onCompleted after you set
>> the value. In onValueChanged, you check if the bool is true before doing
>> anything on the value.
>>
>> Bo.
>>
>> --
>> Viking Software
>> Qt and C++ developers for hire
>> http://www.vikingsoft.eu <http://www.vikingsoft.eu/>
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>> http://lists.qt-project.org/mailman/listinfo/interest
>


-- 
Viking Software
Qt and C++ developers for hire
http://www.vikingsoft.eu




More information about the Interest mailing list