用QML語言開發MeeGo應用程序在線開發教程 – MeeGo開發中文網

用QML語言開發MeeGo應用程序在線開發教程 – MeeGo開發中文網

用QML語言開發MeeGo應用程序在線教程 – MeeGo開發中文網css

Qt Declarative UI 傳得沸沸揚揚,卻不多有中文資料介紹這是一個什麼樣的技術,以及如何使用它。偶爾能搜到幾篇也是掐頭去尾的,讓人摸不着頭腦。這個入門教程來自於Qt官方文檔,更多的是語法性的介紹。html

什麼是QML?

QML是一種描述應用程序UI的聲明式語言、腳本語言,文件格式以.qml結尾包括應用程序的外觀(菜單、按鈕、佈局等)以及行爲(點擊事件)的描述。在QML中,UI界面被描述成一種樹狀的帶屬性對象的結構。若是對HTML和CSS等Web技術有所理解是會有幫助的,但不是必需的。語法格式很是像CSS(參考後文具體例子),但又支持javacript形式的編程控制。java

上面是官方介紹的前兩段,QML其實是Qt Quick(Qt4.7.0中的新特性)核心組件之一:Qt Quick是一組旨在幫助開發者建立在移動電話,媒體播放器,機頂盒和其餘便攜設備上使用愈來愈多的直觀、現代、流暢UI的工具集合。Qt Quick包括一組豐富的用戶界面元素,一種用於描述用戶界面的聲明性語言(QML)及運行時,一組用於將這些高層次特性集成到經典的Qt應用程序的 C++ API。git

從官方的介紹能夠看出,Qt Quick是爲移動平臺快速開發所量身打造的,先看一個實際例子:在MeeGo上運行的MeeNotes,除了業務邏輯,界面UI都是使用QML實現的web

MeeNotes運行效果
MeeNotes運行效果編程

橫豎屏幕切換
橫豎屏幕切換app

在模擬器中運行效果
在模擬器中運行效果dom

MeeNotes能夠在這裏找到:使用git把qt-components和meenotes clone下來,而後先編譯qt-components,這個依賴於qt4.7,是使用QML快速開發meego應用程序的關鍵,它實現了一套meego的QML Module,以後能夠編譯運行下MeeNotes,若是運行不了,請檢查Qt安裝目錄裏是否有 com.nokia.meego這個module(個人位於/usr/local/Trolltech/Qt-4.7.0/imports/com /meego)若是沒有,則須要在qt-components解壓目錄下的 src/MeeGo 手動執行qmake/make/make install,進行編譯安裝。工具

簡單看看MeeNotes下的實際代碼

src目錄下的src.pro,紅色部分便是與使用libmeegotouch開發所不一樣之處 :佈局

 
  1. TEMPLATE = app
  2. TARGET = ../MeeNotes
  3. LIBS += -lQtComponents
  4. HEADERS += models/meenotesmodel.h \
  5.           models/notemodel.h
  6. SOURCES += main.cpp \
  7.           models/meenotesmodel.cpp \
  8.           models/notemodel.cpp
  9. QT += declarative

再看主入口main.cpp:

 
  1. #include "models/meenotesmodel.h"
  2. #include <QApplication>
  3. #include <QDeclarativeContext>
  4. #include <QDeclarativeComponent>
  5. #include <QDir>
  6. #include <QtComponents/qdeclarativewindow.h>
  7. int main(int argc, char **argv)
  8. {
  9.         QApplication app(argc, argv);
  10.         app.setApplicationName("MeeNotes");
  11.         //MeeNotesModel 是Model類
  12.         qmlRegisterType<NoteModel>();
  13.         MeeNotesModel model;
  14.         model.setSource("notes/");
  15.         //在哪查找main.qml
  16. #ifndef MEENOTES_RESOURCE_DIR
  17.           const QDir dir(QApplication::applicationDirPath());
  18.           const QUrl qmlPath(dir.absoluteFilePath("resource/default/main.qml"));
  19. #else
  20.           const QDir dir(MEENOTES_RESOURCE_DIR);
  21.           const QUrl qmlPath(dir.absoluteFilePath("main.qml"));
  22. #endif
  23.         //建立可以解釋qml運行的window
  24.         QDeclarativeWindow window(qmlPath);
  25.          window.rootContext()->setContextProperty("meenotes", &model);
  26.          window.window()->show();
  27.          return app.exec();
  28. }

查看main.qml:

 
  1. import Qt 4.7
  2. import com.meego 1.0
  3. Window {
  4.    id: window
  5.    Component.onCompleted: {
  6.        window.nextPage(Qt.createComponent("NoteList.qml"))
  7.    }
  8. }

查看NoteList.qml:

 
  1. import Qt 4.7
  2. import com.meego 1.0
  3. Page {
  4.    title: "MeeNotes"
  5.    actions: [
  6.        Action {
  7.            iconId: "icon-m-toolbar-add" //新建note按鈕的處理
  8.            onTriggered: {
  9.                var note = meenotes.newNote();
  10.                note.title = (Math.random() > 0.5) ? "Cool title!" : "";
  11.                viewNote(note);
  12.            }
  13.        },
  14.        Action {
  15.            iconId: "icon-m-toolbar-favorite-mark" //橫豎屏切換的按鈕處理
  16.            onTriggered: {
  17.                screenscreen.orientation = screen.orientation == Screen.Portrait ? Screen.Landscape : Screen.Portrait;
  18.            }
  19.        }
  20.    ]
  21.    Component {
  22.         … … …//省略
  23.    }
  24.    Rectangle {
  25.        color: "white"
  26.        anchors.fill: parent
  27.    }
  28.    GridView {
  29.        id: grid
  30.        anchors.fill: parent
  31.        model: meenotes
  32.        cellWidth: 250
  33.        cellHeight: 210
  34.        delegate: noteDelegate
  35.    }
  36.    function viewNote(note) {
  37.        window.nextPage(Qt.createComponent("Note.qml"));
  38.        window.currentPage.note = note;
  39.    }
  40. }

鑑於QML相似於web網頁css的編寫方式,效率已經很高了,可是彷佛Qt Designer被咱們忽視了,其實2.01版的Desinger已經可使用meegotouch風格進行預覽了,效果以下圖:

效果圖
效果圖

目前Designer並不能直接生成QML文件,下一個版本的Desinger以及在計劃之中了,能夠叫他Qt Quick Designer,在Qt Roadmap中已經能夠體現出來了:

Qt Quick Designer

Qt Quick Designer

這即是用QML語言開發MeeGo應用程序的教程。

相關文章
相關標籤/搜索