QML语言开发MeeGo应用程序示例

本文详细解读了MeeNotes项目的源代码结构,包括src目录下的src.pro配置文件、主入口main.cpp以及核心qml文件main.qml的用法。通过分析代码,了解了如何使用libmeegotouch开发并构建一个简单的MeeGo应用程序。同时展示了如何在qml中注册和使用自定义模型类,并在主窗口中展示应用界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 简单看看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.     
  12.         //MeeNotesModel 是Model类  
  13.         qmlRegisterType<NoteModel>();    
  14.         MeeNotesModel model;    
  15.         model.setSource("notes/");  
  16.         //在哪查找main.qml  
  17. #ifndef MEENOTES_RESOURCE_DIR    
  18.           const QDir dir(QApplication::applicationDirPath());    
  19.           const QUrl qmlPath(dir.absoluteFilePath("resource/default/main.qml"));     
  20. #else    
  21.           const QDir dir(MEENOTES_RESOURCE_DIR);    
  22.           const QUrl qmlPath(dir.absoluteFilePath("main.qml"));     
  23. #endif    
  24.         //创建能够解释qml运行的window  
  25.         QDeclarativeWindow window(qmlPath);     
  26.          window.rootContext()->setContextProperty("meenotes", &model);    
  27.          window.window()->show();    
  28.          return app.exec();    
  29. }  

查看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.    }    
注意:

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,进行编译安装。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值