QML中的JavaScript用法详解

来源:互联网 发布:新疆4g网络开通时间 编辑:程序博客网 时间:2024/04/28 12:03

熟悉JavaScript的应该都了解Netscape公司,一代骄子虽然倒下了,却给后人留下了最为珍贵的产品和经验,在互联网发展史上享有举足轻重的地位,这里就不讲故事了,虽然很精彩,从未被磨灭。QML是对JavaScript的扩展,提供了JS主机环境,用法相似,但有些地方与浏览器/服务器端提供的JS主机环境(如Node.js)是不同的,用起来又有一些限制,下面列举一些常用的方法。

1QML文件中的JS表达式

初始化时属性绑定——

[plain] view plain copy
  1. // Property.qml  
  2.   
  3. import QtQuick 2.0  
  4.   
  5. Rectangle {  
  6.     id: colorButton  
  7.     width: 360; height: 360  
  8.     color: mouseArea.pressed ? "steelblue" : "lightsteelblue"  
  9.   
  10.     MouseArea {  
  11.         id: mouseArea  
  12.         anchors.fill: parent  
  13.     }  
  14. }  

使用Qt.binding()完成属性绑定——

[plain] view plain copy
  1. // Property2.qml  
  2.   
  3. import QtQuick 2.0  
  4.   
  5. Rectangle {  
  6.     id: colorbutton  
  7.     width: 360; height: 360  
  8.     color: "yellow"  
  9.   
  10.     MouseArea {  
  11.         id: mouseArea  
  12.         anchors.fill: parent  
  13.     }  
  14.   
  15.     Component.onCompleted: {  
  16.         color = Qt.binding(function() { return mouseArea.pressed ? "red" : "green" })  
  17.     }  
  18. }  

信号处理中的JS表达式——

[plain] view plain copy
  1. // Handler.qml  
  2.   
  3. import QtQuick 2.0  
  4.   
  5. Rectangle {  
  6.     id: button  
  7.     width: 200; height: 100; color: "lightblue"  
  8.   
  9.     MouseArea {  
  10.         id: mouseArea  
  11.         anchors.fill: parent  
  12.         onPressed: label.text = "I am Pressed!"  
  13.         onReleased: label.text = "Click Me!"  
  14.     }  
  15.   
  16.     Text {  
  17.         id: label  
  18.         anchors.centerIn: parent  
  19.         text: "Press Me!"  
  20.     }  
  21. }  

QML文件中函数的JS表达式——

[plain] view plain copy
  1. // InlineFunction.qml  
  2.   
  3. import QtQuick 2.0  
  4.   
  5. Item {  
  6.     function factorial(a) {  
  7.         a = parseInt(a);  
  8.         if (a <= 0)  
  9.             return 1;  
  10.         else  
  11.             return a * factorial(a - 1);  
  12.     }  
  13.   
  14.     MouseArea {  
  15.         anchors.fill: parent  
  16.         onClicked: console.log(factorial(5))  
  17.     }  
  18. }  

JS文件中函数的JS表达式——

[plain] view plain copy
  1. // factorial.js  
  2.   
  3. function factorial(a) {  
  4.     a = parseInt(a);  
  5.     if (a <= 0)  
  6.         return 1;  
  7.     else  
  8.         return a * factorial(a - 1);  
  9. }  
  10.   
  11. // ExternalFunction.qml  
  12.   
  13. import QtQuick 2.0  
  14. import "factorial.js" as MathFunctions  
  15.   
  16. Item {  
  17.     MouseArea {  
  18.         anchors.fill: parent  
  19.         onClicked: console.log(MathFunctions.factorial(10))  
  20.     }  
  21. }  

使用connect()处理信号——

[plain] view plain copy
  1. // Connecting.qml  
  2.   
  3. import QtQuick 2.0  
  4. import "script.js" as MyScript  
  5.   
  6. Item {  
  7.     id: item  
  8.     width: 360; height: 360  
  9.   
  10.     MouseArea {  
  11.         id: mouseArea  
  12.         anchors.fill: parent  
  13.     }  
  14.   
  15.     Component.onCompleted: {  
  16.         mouseArea.clicked.connect(MyScript.jsFunction)  
  17.     }  
  18. }  
  19.   
  20. // script.js  
  21.   
  22. function jsFunction() {  
  23.     console.log("Called JavaScript function!")  
  24. }  

使用Component.onCompleted附加信号

[plain] view plain copy
  1. // Startup.qml  
  2.   
  3. import QtQuick 2.0  
  4.   
  5. Rectangle {  
  6.     function startupFunction() {  
  7.         console.log("startFunction called")  
  8.     }  
  9.   
  10.     Component.onCompleted: startupFunction()  
  11. }  

2QML文件中的JS资源

用独立的JS文件实现QML逻辑部分——

[plain] view plain copy
  1. // MyButton.qml  
  2.   
  3. import QtQuick 2.0  
  4. import "my_button_impl.js" as Logic  
  5.   
  6. Rectangle {  
  7.     id: rect  
  8.     width: 360  
  9.     height: 360  
  10.     color: "red"  
  11.   
  12.     MouseArea {  
  13.         id: mousearea  
  14.         anchors.fill: parent  
  15.         onClicked: Logic.onClicked(rect)  
  16.     }  
  17. }  
  18.   
  19. // my_button_impl.js  
  20.   
  21. var clickCount = 0;  
  22.   
  23. function onClicked(btn) {  
  24.     clickCount += 1;  
  25.     if ((clickCount % 5) == 0) {  
  26.         btn.color = Qt.rgba(1,0,0,1);  
  27.     } else {  
  28.         btn.color = Qt.rgba(0,1,0,1);  
  29.     }  
  30. }  

JS文件定义为共享库——

[plain] view plain copy
  1. // Calculator.qml  
  2.   
  3. import QtQuick 2.0  
  4. import "factorial.js" as FactorialCalculator  
  5.   
  6. Text {  
  7.     width: 500  
  8.     height: 100  
  9.     property int input: 10  
  10.     text: "The factorial of " + input + " is: " + FactorialCalculator.factorial(input)  
  11. }  
  12.   
  13. // factorial.js  
  14.   
  15. .pragma library  
  16.   
  17. var factorialCount = 0;  
  18.   
  19. function factorial(a) {  
  20.     a = parseInt(a);  
  21.     if (a > 0)  
  22.         return a * factorial(a - 1);  
  23.     factorialCount += 1;  
  24.     return 1;  
  25. }  
  26.   
  27. function factorialCallCount() {  
  28.     return factorialCount;  
  29. }  

使用WorkerScript这个QML类型——

[plain] view plain copy
  1. // MyWorkerScript.qml  
  2.   
  3. import QtQuick 2.0  
  4.   
  5. Rectangle {  
  6.     width: 300; height: 300  
  7.   
  8.     Text {  
  9.         id: myText  
  10.         text: 'Click anywhere'  
  11.     }  
  12.   
  13.     WorkerScript {  
  14.         id: myWorker  
  15.         source: "worker_script.js"  
  16.         onMessage: myText.text = messageObject.reply  
  17.     }  
  18.   
  19.     MouseArea {  
  20.         anchors.fill: parent  
  21.         onClicked: myWorker.sendMessage({ 'x': mouse.x, 'y': mouse.y })  
  22.     }  
  23. }  
  24.   
  25. // worker_script.js  
  26.   
  27. WorkerScript.onMessage = function(message) {  
  28.     WorkerScript.sendMessage({ 'reply': 'Mouse is at ' + message.x + ',' + message.y })  
  29. }  

3、导入JS文件

JS文件中导入另一个JS文件——

[plain] view plain copy
  1. .import “filename.js” as Qualifier  

JS文件中导入QML模块——

[plain] view plain copy
  1. .import TypeNamespace MajorVersion.MinorVersion as Qualifier  

JS文件中使用Qt.include()来导入另一个JS文件——

[plain] view plain copy
  1. // Including.qml  
  2.   
  3. import QtQuick 2.0  
  4. import "script2.js" as MyScript  
  5.   
  6. Item {  
  7.     width: 360; height: 360  
  8.   
  9.     MouseArea {  
  10.         anchors.fill: parent  
  11.         onClicked: {  
  12.             MyScript.showCalculations(10)  
  13.             console.log("Call factorial() from QML:", MyScript.factorial(10))  
  14.         }  
  15.     }  
  16. }  
  17.   
  18. // script2.js  
  19.   
  20. Qt.include("factorial2.js")  
  21.   
  22. function showCalculations(value) {  
  23.     console.log("Call factorial() from script2.js:", factorial(value));  
  24. }  
  25.   
  26. // factorial2.js  
  27.   
  28. function factorial(a) {  
  29.     a = parseInt(a);  
  30.     if (a <= 0)  
  31.         return 1;  
  32.     else  
  33.         return a * factorial(a - 1);  

  1. }  

转载:http://blog.csdn.net/ieearth/article/details/42105615

0 0
原创粉丝点击