qt-quick-for-designers-1:qt_quick_and_javascript
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
qt-quick-for-designers-1:qt_quick_and_javascript [2013/06/22 03:27] – created mithat | qt-quick-for-designers-1:qt_quick_and_javascript [2013/07/06 22:53] (current) – [Importing a Javascript file] mithat | ||
---|---|---|---|
Line 18: | Line 18: | ||
} | } | ||
}</ | }</ | ||
+ | |||
+ | ===== Javascript functions ===== | ||
* You can add a Javascript function anywhere in your Qt Quick file. | * You can add a Javascript function anywhere in your Qt Quick file. | ||
- | * This is a simple | + | * The following defines and uses a function that picks a number and references it in an array of states. |
- | <code javascript> | + | * FIXME '' |
+ | {{youtube> | ||
+ | <file javascript javascript-example.qml> | ||
+ | import QtQuick 1.0 | ||
+ | |||
+ | Item { | ||
+ | id: myItem | ||
+ | width: 400 | ||
+ | height: 400 | ||
+ | |||
+ | property int currentStateNumber: | ||
+ | |||
+ | function randomState() | ||
+ | { | ||
+ | var statesArray = [' | ||
+ | var randomNumber; | ||
+ | do { | ||
+ | randomNumber = Math.floor(Math.random()*statesArray.length); | ||
+ | } while (randomNumber == currentStateNumber); | ||
+ | currentStateNumber = randomNumber; | ||
+ | return statesArray[randomNumber]; | ||
+ | } | ||
+ | |||
+ | Image { | ||
+ | source: " | ||
+ | } | ||
+ | |||
+ | Image { | ||
+ | id: spaceship | ||
+ | x: -200 | ||
+ | y: 50 | ||
+ | source: " | ||
+ | } | ||
+ | |||
+ | Image { | ||
+ | id: button | ||
+ | source: " | ||
+ | anchors.horizontalCenter: | ||
+ | anchors.bottom: | ||
+ | anchors.bottomMargin: | ||
+ | |||
+ | Text { | ||
+ | id: label | ||
+ | text: " | ||
+ | color: " | ||
+ | anchors.horizontalCenter: | ||
+ | anchors.top: | ||
+ | anchors.topMargin: | ||
+ | } | ||
+ | |||
+ | MouseArea { | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 250 | ||
+ | y: 200 | ||
+ | } | ||
+ | }, | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 100 | ||
+ | y: 200 | ||
+ | } | ||
+ | }, | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 10 | ||
+ | y: 50 | ||
+ | } | ||
+ | }, | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 100 | ||
+ | y: 50 | ||
+ | } | ||
+ | } | ||
+ | ] | ||
+ | |||
+ | transitions: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | }, | ||
+ | | ||
+ | from: "" | ||
+ | to: " | ||
+ | | ||
+ | PauseAnimation { | ||
+ | | ||
+ | } | ||
+ | |||
+ | NumberAnimation { | ||
+ | properties: " | ||
+ | duration: 500 | ||
+ | easing.type: | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | ] | ||
+ | |||
+ | Component.onCompleted: | ||
+ | myItem.state = " | ||
+ | } | ||
+ | |||
+ | }</ | ||
+ | |||
+ | ===== Importing a Javascript file ===== | ||
+ | * If you want to organize your code, you can import a js file to Qt Quick: | ||
+ | < | ||
function randomState() | function randomState() | ||
{ | { | ||
- | | + | var statesArray = ['topLeft', 'topRight', 'bottomLeft', 'bottomRight']; |
- | var randomNumber = Math.floor(Math.random()*statesArray.length); | + | var randomNumber; |
- | return statesArray[randomNumber]; | + | do { |
- | }</code> | + | randomNumber = Math.floor(Math.random()*statesArray.length); |
+ | } while (randomNumber == currentStateNumber); | ||
+ | currentStateNumber = randomNumber; | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <file javascript importing-javascript.qml> | ||
+ | import QtQuick 1.0 | ||
+ | |||
+ | import " | ||
+ | |||
+ | Item { | ||
+ | id: myItem | ||
+ | width: 400 | ||
+ | height: 400 | ||
+ | |||
+ | property int currentStateNumber: | ||
+ | |||
+ | Image { | ||
+ | source: " | ||
+ | } | ||
+ | |||
+ | Image { | ||
+ | id: spaceship | ||
+ | x: -200 | ||
+ | y: 50 | ||
+ | source: " | ||
+ | } | ||
+ | |||
+ | Image { | ||
+ | id: button | ||
+ | source: " | ||
+ | anchors.horizontalCenter: | ||
+ | anchors.bottom: | ||
+ | anchors.bottomMargin: | ||
+ | |||
+ | Text { | ||
+ | id: label | ||
+ | text: " | ||
+ | color: " | ||
+ | anchors.horizontalCenter: | ||
+ | anchors.top: | ||
+ | anchors.topMargin: | ||
+ | } | ||
+ | |||
+ | MouseArea { | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 250 | ||
+ | y: 200 | ||
+ | } | ||
+ | }, | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 100 | ||
+ | y: 200 | ||
+ | } | ||
+ | }, | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 10 | ||
+ | y: 50 | ||
+ | } | ||
+ | }, | ||
+ | State { | ||
+ | name: " | ||
+ | | ||
+ | | ||
+ | x: 100 | ||
+ | y: 50 | ||
+ | } | ||
+ | } | ||
+ | ] | ||
+ | |||
+ | transitions: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | }, | ||
+ | | ||
+ | from: "" | ||
+ | to: " | ||
+ | | ||
+ | PauseAnimation { | ||
+ | | ||
+ | } | ||
+ | |||
+ | NumberAnimation { | ||
+ | properties: " | ||
+ | duration: 500 | ||
+ | easing.type: | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | ] | ||
+ | |||
+ | Component.onCompleted: | ||
+ | myItem.state = " | ||
+ | } | ||
+ | |||
+ | }</file> | ||
+ | -------------------------------------------------------------- | ||
+ | ====== Resources ====== | ||
+ | * {{: | ||
+ | * All source code is subject to this [[copyright header|copyright]]. | ||
qt-quick-for-designers-1/qt_quick_and_javascript.1371871639.txt.gz · Last modified: 2013/06/22 03:27 by mithat