简单看看MeeNotes下的实际代码
src目录下的src.pro,红色部分即是与使用libmeegotouch开发所不同之处 :
- TEMPLATE = app
- TARGET = ../MeeNotes
- LIBS += -lQtComponents
- HEADERS += models/meenotesmodel.h \
- models/notemodel.h
- SOURCES += main.cpp \
- models/meenotesmodel.cpp \
- models/notemodel.cpp
- QT += declarative
再看主入口main.cpp:
- #include "models/meenotesmodel.h"
- #include <QApplication>
- #include <QDeclarativeContext>
- #include <QDeclarativeComponent>
- #include <QDir>
- #include <QtComponents/qdeclarativewindow.h>
- int main(int argc, char **argv)
- {
- QApplication app(argc, argv);
- app.setApplicationName("MeeNotes");
- //MeeNotesModel 是Model类
- qmlRegisterType<NoteModel>();
- MeeNotesModel model;
- model.setSource("notes/");
- //在哪查找main.qml
- #ifndef MEENOTES_RESOURCE_DIR
- const QDir dir(QApplication::applicationDirPath());
- const QUrl qmlPath(dir.absoluteFilePath("resource/default/main.qml"));
- #else
- const QDir dir(MEENOTES_RESOURCE_DIR);
- const QUrl qmlPath(dir.absoluteFilePath("main.qml"));
- #endif
- //创建能够解释qml运行的window
- QDeclarativeWindow window(qmlPath);
- window.rootContext()->setContextProperty("meenotes", &model);
- window.window()->show();
- return app.exec();
- }
查看main.qml:
- import Qt 4.7
- import com.meego 1.0
- Window {
- id: window
- Component.onCompleted: {
- window.nextPage(Qt.createComponent("NoteList.qml"))
- }
- }
查看NoteList.qml:
- import Qt 4.7
- import com.meego 1.0
- Page {
- title: "MeeNotes"
- actions: [
- Action {
- iconId: "icon-m-toolbar-add" //新建note按钮的处理
- onTriggered: {
- var note = meenotes.newNote();
- note.title = (Math.random() > 0.5) ? "Cool title!" : "";
- viewNote(note);
- }
- },
- Action {
- iconId: "icon-m-toolbar-favorite-mark" //横竖屏切换的按钮处理
- onTriggered: {
- screenscreen.orientation = screen.orientation == Screen.Portrait ? Screen.Landscape : Screen.Portrait;
- }
- }
- ]
- Component {
- … … …//省略
- }
- Rectangle {
- color: "white"
- anchors.fill: parent
- }
- GridView {
- id: grid
- anchors.fill: parent
- model: meenotes
- cellWidth: 250
- cellHeight: 210
- delegate: noteDelegate
- }
- function viewNote(note) {
- window.nextPage(Qt.createComponent("Note.qml"));
- window.currentPage.note = note;
- }
注意:
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,进行编译安装。