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.

如何在Symbian上制作和使用QML extension plugin

From Wiki
Jump to: navigation, search
Article Metadata

代码示例
兼容于
平台:
Symbian

文章
zhouhl 在 16 Jan 2011 创建
最后由 hamishwillee 在 11 Oct 2012 编辑


Contents

引言

本文介绍如何制作QML插件,以及如何在Qt Quick (QML)应用程序中使用QML插件。


如何创建QML extension plugin

创建QML插件,我们需要做以下步骤:

  • 注册我们QML类型的插件类
  • 定义插件的工程文件(.pro)
  • 告知QML engine 加载插件的qmldir 文件

首先我们创建一个插件类QExampleQmlPlugin。这个类必须要从QDeclarativeExtensionPlugin 继承下来,并且要使用registerTypes虚方法注册我们自己的QML类型,最后还要使用Q_EXPORT_PLUGIN2() 导出插件。

//![plugin]
class QExampleQmlPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
void registerTypes(const char *uri)
{
qmlRegisterType<TimeModel>("com.nokia.TimeExample", 1, 0, "Time");
}
};
//![plugin]
 
//![export]
Q_EXPORT_PLUGIN2(qtqmltplugins, QExampleQmlPlugin);
//![export]

然后我们需要有一个定义插件的Qt工程文件

TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
 
#destination dir that is added to the installation path
DESTDIR = com/nokia/TimeExample
 
#Name of our dll qtqmltplugins.dll
TARGET = qtqmltplugins
 
SOURCES += plugin.cpp
 
#qdeclarativesources holds all the necessary file that we install in the devices
qdeclarativesources.files += \
com/nokia/TimeExample/qmldir
 
#List of resources that will be installed
INSTALLS += qdeclarativesources target
 
 
# On Symbian, define DEPLOYMENT to generate a SIS file with all the necessary files
# The correct location to deploy plug-in files on Symbian is under /resource/qt/imports/
symbian {
TARGET.EPOCALLOWDLLDATA = 1
pluginstub.sources = $${TARGET}.dll
pluginstub.path = /resource/qt/imports/$$DESTDIR
resources.path = /resource/qt/imports/$$DESTDIR
resources.sources += $${qdeclarativesources.files}
#Specifies which additional files will be deployed.
DEPLOYMENT += pluginstub resources
}

最后我们要制作一个告知QML engine 加载插件的qmldir 文件:

#plugin <Name> [<Path>]
plugin qtqmltplugins

在本例程中,为了隐藏插件中的qml文件,我们将他们放入了qrc文件中,具体请参考例子程序。

创建使用QML plugin的Qt Quick应用程序

我们使用Qt Creator 向导新建一个Qt Quick application,这将会生成一个新的main.qml,其具体内容如下:

import QtQuick 1.0
import com.nokia.TimeExample 1.0
 
Clock
{ // this class is defined in QML (Clock.qml)
width: 360
height: 360
Time { // this class is defined in C++ (plugin.cpp)
id: time
}
hours: time.hour
minutes: time.minute
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}

这里我们使用import com.nokia.TimeExample 1.0 将插件所在的模块引用进来,这样我们就可以使用插件中提供的Time以及 Clock 这些element了。

真机上测试

首先将我们的插件(qtqmltplugins.sis)安装到手机,然后安装我们测试插件的程序(qtqmltpluginstest.sis)并运行就可以了。我们可以看到一个时钟显示在屏幕上,当我们点击屏幕过后程序就退出了。

例程

Media:symbianqmltplugins.zip

Media:symbianqmltpluginstest.zip

相关链接

This page was last modified on 11 October 2012, at 01:20.
230 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.

×