用QML语言开发MeeGo应用程序在线开发教程 – MeeGo开发中文网

用QML语言开发MeeGo应用程序在线开发教程 – MeeGo开发中文网

用QML语言开发MeeGo应用程序在线教程 – MeeGo开发中文网

Qt Declarative UI 传得沸沸扬扬,却很少有中文资料介绍这是一个什么样的技术,以及如何使用它。偶尔能搜到几篇也是掐头去尾的,让人摸不着头脑。这个入门教程来自于Qt官方文档,更多的是语法性的介绍。

什么是QML?

QML是一种描述应用程序UI的声明式语言、脚本语言,文件格式以.qml结尾包括应用程序的外观(菜单、按钮、布局等)以及行为(点击事件)的描述。在QML中,UI界面被描述成一种树状的带属性对象的结构。如果对HTML和CSS等Web技术有所理解是会有帮助的,但不是必需的。语法格式非常像CSS(参考后文具体例子),但又支持javacript形式的编程控制。

上面是官方介绍的前两段,QML实际上是Qt Quick(Qt4.7.0中的新特性)核心组件之一:Qt Quick是一组旨在帮助开发者创建在移动电话,媒体播放器,机顶盒和其他便携设备上使用越来越多的直观、现代、流畅UI的工具集合。Qt Quick包括一组丰富的用户界面元素,一种用于描述用户界面的声明性语言(QML)及运行时,一组用于将这些高层次特性集成到经典的Qt应用程序的 C++ API。

从官方的介绍可以看出,Qt Quick是为移动平台快速开发所量身打造的,先看一个实际例子:在MeeGo上运行的MeeNotes,除了业务逻辑,界面UI都是使用QML实现的

MeeNotes运行效果
MeeNotes运行效果

横竖屏幕切换
横竖屏幕切换

在模拟器中运行效果
在模拟器中运行效果

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

转载于:https://my.oschina.net/meegoos/blog/13530

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值