用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应用程序的教程。

相关文章
相关标签/搜索