Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

如何制作和使用QML extension plug in

From Wiki
Jump to: navigation, search
Article Metadata

代码示例
兼容于
平台:
Symbian

文章
liuting 在 21 Dec 2011 创建
最后由 renlin 在 11 Jul 2012 编辑


Contents

介绍

本文将介绍如歌创建一个QML extension plug in 以及如何在QML 中使用 plug in


如何制作QML Extension plug in

为了创建一个能在QML 中使用的PLUG IN ,我们必须执行以下步骤: 1定义一个PLUG IN CLASS 用于注册 QML TYPE 2 设置.PRO文件 3定义qmldir文件用于告诉QMLengine 从哪里加载 PLUG IN 下面我们看下具体的实现: 首先我们定义一个类继承于QDeclarativeExtensionPlugin,并在类中实现registerTypes()方法,用以注册QML 类型,具体代码如下:

    #include <QDeclarativeExtensionPlugin>
class ChartsPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
void registerTypes(const char *uri);
};
    #include "piechart.h"
#include "pieslice.h"
#include <qdeclarative.h>
void ChartsPlugin::registerTypes(const char *uri)
{
qmlRegisterType<PieChart>(uri, 1, 0, "PieChart");
qmlRegisterType<PieSlice>(uri, 1, 0, "PieSlice");
}
Q_EXPORT_PLUGIN2(chartsplugin, ChartsPlugin);

接下来我们需要设置.PRO文件,具体代码如下:

    TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR = lib
OBJECTS_DIR = tmp
MOC_DIR = tmp
HEADERS += piechart.h \
pieslice.h \
chartsplugin.h
SOURCES += piechart.cpp \
pieslice.cpp \
chartsplugin.cpp
symbian {
include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)
TARGET.EPOCALLOWDLLDATA = 1
}
maemo5: include($$QT_SOURCE_TREE/examples/maemo5pkgrules.pri)

需要强调一点的是DESTDIR = lib指定了PLUG IN的将产生在lib 文件夹中 最后我们需要定义一个qmldir文件,具体代码如下:

plugin chapter6-plugins lib

上述代码告诉QML ENGINE 去 lib 文件夹下加载名为chapter6-plugins 的 PLUG IN,接下来我们看下如何在QML中使用这个PLUG IN

如何在QML 中使用 PLUG IN

代码如下:

import QtQuick 1.0
Item {
width: 300; height: 200
 
PieChart {
anchors.centerIn: parent
width: 100; height: 100
 
slices: [
PieSlice {
anchors.fill: parent
color: "red"
fromAngle: 0; angleSpan: 110
},
PieSlice {
anchors.fill: parent
color: "black"
fromAngle: 110; angleSpan: 50
},
PieSlice {
anchors.fill: parent
color: "blue"
fromAngle: 160; angleSpan: 100
}
]
}
}

代码下载

File:Testqmlplugin.zip

相关链接

This page was last modified on 11 July 2012, at 03:58.
252 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×