QML學習

 QML是一種描述性的腳本語言,文件格式以.qml結尾。語法格式非常像CSS(參考後文具體例子),支持與C++代碼進行交互。是Qt推出的Qt Quick技術的一部分,可以在QT creator中以可視化的方式編輯.qml文件,如下所示:(注:我用的環境爲Ubuntu 12.04 + QT)

下面來寫一個簡單的QML示例程序:

1>用QT creator創建一個QML應用程序

工程名設爲:qmlTest

創建完成後工程目錄如下:

2>編寫代碼

我將要實現的界面及功能如下:

界面包含一個label和一個按鈕,當我點擊quit按鈕時程序退出。

首先我們編寫按鈕的qml實現文件,在工程的qml/qmlTest目錄下添加一個qml文件Button.qml

  1. import QtQuick 1.0 
  2.  
  3. Rectangle { 
  4.  
  5.     //identifier of the item 
  6.     id: button 
  7.  
  8.     property string label: "button label" 
  9.  
  10.     //these properties act as constants, useable outside this QML file 
  11.     property int buttonHeight: 75 
  12.     property int buttonWidth: 150 
  13.      
  14.     //the color highlight when the mouse hovers on the rectangle 
  15.     property color onHoverColor: "gold" 
  16.     property color borderColor: "white" 
  17.  
  18.     //buttonColor is set to the button's main color 
  19.     property color buttonColor: "lightblue"     
  20.  
  21.     //set appearance properties 
  22.     radius:10 
  23.     smooth: true 
  24.     border{color: "white"; width: 3} 
  25.     width: buttonWidth; height: buttonHeight 
  26.      
  27.     Text{ 
  28.         id: buttonLabel 
  29.         anchors.centerIn: parent 
  30.         text: label 
  31.     } 
  32.  
  33.     //buttonClick() is callable and a signal handler, onButtonClick is automatically created 
  34.     signal buttonClick() 
  35.     onButtonClick: { 
  36.         console.log(buttonLabel.text + " clicked" ) 
  37.     } 
  38.  
  39.     //define the clickable area to be the whole rectangle 
  40.     MouseArea{  
  41.         id: buttonMouseArea 
  42.         anchors.fill: parent    //stretch the area to the parent's dimension 
  43.         onClicked: buttonClick() 
  44.               
  45.         //if true, then onEntered and onExited called if mouse hovers in the mouse area 
  46.                 //if false, a button must be clicked to detect the mouse hover 
  47.                 hoverEnabled: true 
  48.  
  49.                 //display a border if the mouse hovers on the button mouse area 
  50.                 onEntered: parent.border.color = onHoverColor 
  51.                 //remove the border if the mouse exits the button mouse area 
  52.                 onExited:  parent.border.color = borderColor 
  53.          
  54.     } 
  55.  
  56.     //change the color of the button when pressed 
  57.     color: buttonMouseArea.pressed ? Qt.darker(buttonColor, 1.5) : buttonColor 

 修改main.qml的內容以使用剛創建的Button組件,如下:

  1. import QtQuick 1.1 
  2.  
  3. Rectangle { 
  4.     width: 360 
  5.     height: 360 
  6.     Text { 
  7.         text: qsTr("Hello World"
  8.     } 
  9.  
  10.     Button { 
  11.         anchors.right: parent.right 
  12.         anchors.top: parent.top 
  13.         anchors.margins: 10 
  14.         buttonWidth: 100 
  15.         buttonHeight: 50 
  16.         label: "Quit" 
  17.         onButtonClick: { 
  18.             Qt.quit(); 
  19.         } 
  20.     } 

接下來就可以在C++代碼中進行調用並顯示出來了。

main.cpp:

  1. #include <QApplication> 
  2. #include "qmlapplicationviewer.h" 
  3.  
  4. Q_DECL_EXPORT int main(int argc, char *argv[]) 
  5.     QScopedPointer<QApplication> app(createApplication(argc, argv)); 
  6.  
  7.     QmlApplicationViewer viewer; 
  8.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); 
  9.     viewer.setMainQmlFile(QLatin1String("qml/qmlTest/main.qml")); 
  10.     viewer.showExpanded(); 
  11.  
  12.     return app->exec(); 

點擊運行就可以看到所要的效果了。

有關QML與C++進行交互的詳細介紹可以參考如下鏈接:

http://www.developer.nokia.com/Community/Wiki/QML%E4%B8%8EQt_C%2B%2B_%E4%BA%A4%E4%BA%92%E6%9C%BA%E5%88%B6%E6%8E%A2%E8%AE%A8%E4%B8%8E%E6%80%BB%E7%BB%93

完。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章