Bindings bug or feature
-
I found a situation, where bindings works not as I expected. If I bind variable someVar to some other variable(s) and also add handler onSomeVarChanged, the handler executes before bindings. Thus, using any non-trivial code in handler onSomeVarChanged is potentially dangerious. You can use some component, that has variable binded to someVar and this variable is not updated yet. See example below:
main.qml
@
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1ApplicationWindow {
id: windowproperty var currentDate: null width: 640 height: 480 onCurrentDateChanged: { print("window.currentDate = " + currentDate) print("requestManager.date = " + requestManager.date) requestManager.requestData() } RequestManager { id: requestManager date: currentDate } Button { text: "Click Me!" onClicked: currentDate = new Date }
}
@RequestManager.qml
@
import QtQuick 2.0Item {
property var date; function requestData() { print("requesting data for date " + date) }
}
@Output (after button click):
@
qml: window.currentDate = Пт сен 19 13:26:24 2014 GMT+0300
qml: requestManager.date = null
qml: requesting data for date null
@ -
Of course, there are a lot of workarounds. For example:
main.qml
@
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1ApplicationWindow {
id: windowproperty var currentDate: null width: 640 height: 480 function setCurrentDate(newDate) { currentDate = newDate requestManager.requestData() } RequestManager { id: requestManager date: currentDate } Button { text: "Click Me!" onClicked: setCurrentDate(new Date) }
}
@ -
how about this? Is it better?
@
ApplicationWindow {
id: windowproperty var currentDate: null width: 640 height: 480 RequestManager { id: requestManager date: currentDate onDateChanged: { print("window.currentDate = " + currentDate) print("requestManager.date = " + date) requestData() } } Button { text: "Click Me!" onClicked: currentDate = new Date }
}
@ -
Hi,
One important feature of a declarative language is that you don't know in which order the declarations will be applied in, or the order the side-effects (signal handlers, binding expressions) will be evaluated in. In practice, there is an order defined by the engine implementation, but it is best not to rely on that, as it is an implementation detail rather than a well-specified behaviour (ie, API contract).
To ensure that an imperative expression is evaluated after a binding evaluation occurs, you can trigger the imperative function via a Timer started as a side-effect of the binding expression.
Example:
@
property bool triggerUpdate: false
property var someProperty: {
var newValue = Math.random();
if (triggerUpdate) {
timer.start();
}
return newValue;
}
Timer {
id: timer
interval: 0 // will trigger upon returning to the Qt Event Loop
onTriggered: {
updateViews() // or whatever
}
}
Component.onCompleted: triggerUpdate = true; // will trigger re-evaluation of someProperty binding, and anything bound to someProperty
@In the preceding case, the updateViews() function is guaranteed to be called after ALL binding expressions involving someProperty have been re-evaluated, since the Timer's onTriggered handler will be invoked at some point in the future via the Qt Event Loop.
I hope this helps!
Cheers,
Chris Adams.http://www.qinetic.com.au - The Qt And QML Experts