[Solved] QML and Javascript
-
Hello,
can someone confirm that these codes are interchangable ?
And what does "Component" mean in Component.onCompleted ?@ import QtQuick 1.0
import "script.js" as MyScriptItem {
id: item
width: 200; height: 200MouseArea { id: mouseArea anchors.fill: parent } Component.onCompleted: { mouseArea.clicked.connect(MyScript.jsFunction) }
}@
and
@ import QtQuick 1.0
import "script.js" as MyScriptItem {
id: item
width: 200; height: 200MouseArea { id: mouseArea anchors.fill: parent onClicked: { MyScript.jsFunction } }
}@
Thank you
-
It's basically interchangeable, behaviour-wise, but it is not identical.
The first one defines a "dynamic slot" (signal handler) for the "completed()" signal emitted by the QDeclarativeComponent object associated with the QML file. That slot will be invoked once (since the completed() signal will only be emitted once), and it will connect the mouse area's clicked() signal to the script's jsFunction function (treating it as a signal handler). That jsFunction will then be invoked every time the mouseArea emits the clicked() signal.
The second one defines a dynamic slot which will be invoked every time the mouse area emits clicked() signal. That dynamic slot, as part of its expression, will then invoke another dynamic function (the jsFunction function).
-
Thank you for this detailed answer.
And what does this "Component" do ?
@ Component.onCompleted: {
mouseArea.clicked.connect(MyScript.jsFunction)
}@
I think, it is clear that onCompleted refers to the Item called "item". So I don't need "Component" here, do I ? Or why is it written there ?An additional question is what I can make with Javascript at all.
I think most are math related tasks as well as an storage for variables.
But what can it be used for, too ? -
Component.onCompleted is an attached property, which means that it does not belong to the Item element but is attached (exposed) to it at run time.
See attached properties doc : http://qt-project.org/doc/qt-4.8/qdeclarativeintroduction.html#attached-properties
See Component.onCompleted doc : http://qt-project.org/doc/qt-4.8/qml-component.html#onCompleted-signal -
Thank you, this made it clear.