ITPub博客

首页 > Linux操作系统 > Linux操作系统 > QT declarative UI 初探(二)

QT declarative UI 初探(二)

原创 Linux操作系统 作者:洛欣 时间:2010-05-18 15:06:59 0 删除 编辑

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

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

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

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

 #include 

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

#include
#include
#include
#include
#include

#include “dataobject.h”

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

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

    QDeclarativeView view;

    QList 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的滋味了。。。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22785983/viewspace-662977/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-11-12

  • 博文量
    111
  • 访问量
    273839