QT declarative UI 初探(二)

 

说明一下,我的操作系统是win7 64位。

接上文,两个疑惑让我很郁闷,有种云深不知处的感觉。于是继续翻阅文档,经过几番查询,我只好确定qml.exe就是文档中所提到的qmlviewer。对于第二个疑问,继续学习。

首先,读到这样一段文档(位置QtDeclarative Module):

To include the definitions of the module’s classes, use the following directive:

 #include <QtDeclarative>

To link against the module, add this line to your qmake .pro file:

 QT += declarative

For more information on the Qt Declarative module, see the Declarative UI documentation.

然后,经过我不懈的努力,在examples/declarative/objectlistmodel 这个示例中找到了线索:

objectlistmodel的pro文件如下(省略无关内容):

TEMPLATE = app
TARGET = objectlistmodel
QT += declarative

# Input
SOURCES += main.cpp \
           dataobject.cpp
HEADERS += dataobject.h
RESOURCES += objectlistmodel.qrc

objectlistmodel的main.cpp文件:

#include <QApplication>

#include <qdeclarativeengine.h>
#include <qdeclarativecontext.h>
#include <qdeclarative.h>
#include <qdeclarativeitem.h>
#include <qdeclarativeview.h>

#include “dataobject.h”

/*
   This example illustrates exposing a QList<QObject*> as a
   model in QML
*/

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);

    QDeclarativeView view;

    QList<QObject*> dataList;
    dataList.append(new DataObject(“Item 1″, “red”));
    dataList.append(new DataObject(“Item 2″, “green”));
    dataList.append(new DataObject(“Item 3″, “blue”));
    dataList.append(new DataObject(“Item 4″, “yellow”));

    QDeclarativeContext *ctxt = view.rootContext();
    ctxt->setContextProperty(“myModel”, QVariant::fromValue(dataList));

    view.setSource(QUrl(“qrc:view.qml”));
    view.show();

    return app.exec();
}

看到了吧,原来是用QDeclarativeView这个东东来装载ui的。。。

哈哈,于是我们可以自己来写一个app,把demos/declarative/twitter这个demo给编译成一个exe文件了。

下面是我的main中的代码:

 QApplication a(argc, argv);

    QDeclarativeView vi;
    vi.setSource(QUrl(“twitter.qml”));
    vi.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    vi.show();
    return a.exec();

ok,第一步我们终于迈出了。下面我们就可以开始慢慢享受qml的滋味了。。。

This entry was posted on Thursday, April 29th, 2010 at 5:07 PM and is filed under Qt技术. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply