扩展插件怎么使用Writing an Extension Plugin

Night12 2011-11-11 05:22:05
我打开了我安装qt后的这个例子, extending中有6个例子,前5个可以编译运行.
这个是第6个例子:
C:\Qt\2010.05\qt\examples\declarative\tutorials\extending\chapter6-plugins

现在我构建了这个例子,生成了一个lib文件夹,里面有chapter6-plugins.dll和chapter6-plugins.a两个文件.
但是我再cmd下却换到C:\Qt\2010.05\qt\examples\declarative\tutorials\extending\chapter6-plugins这个目录,然后用qmlviewer app.xml 运行,却没有任何反应....
谁能告诉我改怎么使用这个dll.或者说完成这个例子.


附原文章:
Chapter 6: Writing an Extension Plugin

Files:

declarative/tutorials/extending/chapter6-plugins/app.qml
declarative/tutorials/extending/chapter6-plugins/chartsplugin.cpp
declarative/tutorials/extending/chapter6-plugins/chartsplugin.h
declarative/tutorials/extending/chapter6-plugins/piechart.cpp
declarative/tutorials/extending/chapter6-plugins/piechart.h
declarative/tutorials/extending/chapter6-plugins/pieslice.cpp
declarative/tutorials/extending/chapter6-plugins/pieslice.h
declarative/tutorials/extending/chapter6-plugins/chapter6-plugins.pro
declarative/tutorials/extending/chapter6-plugins/qmldir
Currently the PieChart and PieSlice types are used by app.qml, which is displayed using a QDeclarativeView in a C++ application. An alternative way to use our QML extension is to create a plugin library to make it available to the QML engine. This allows app.qml to be loaded with the QML Viewer (or some other QML runtime application) instead of writing a main.cpp file and loading our own C++ application.

To create a plugin library, we need:

A plugin class that registers our QML types
A project file that describes the plugin
A qmldir file that tells the QML engine to load the plugin
First, we create a plugin class named ChartsPlugin. It subclasses QDeclarativeExtensionPlugin and registers our QML types in the inherited registerTypes() method. It also calls Q_EXPORT_PLUGIN2 for Qt's plugin system.

Here is the ChartsPlugin definition in chartsplugin.h:

#include <QDeclarativeExtensionPlugin>

class ChartsPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
void registerTypes(const char *uri);
};
And its implementation in chartsplugin.cpp:

#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);
Then, we write a .pro project file that defines the project as a plugin library and specifies with DESTDIR that library files should be built into a "lib" subdirectory:

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
}
Finally, we add a qmldir file that is automatically parsed by the QML engine. In this file, we specify that a plugin named "chapter6-plugin" (the name of the example project) can be found in the "lib" subdirectory:

plugin chapter6-plugins lib
Now we have a plugin, and instead of having a main.cpp and an executable, we can build the project and then load the QML file in the QML Viewer:

qmlviewer app.qml
(On Mac OS X, you can launch the "QMLViewer" application instead.)

Notice the "import Charts 1.0" statement has disappeared from app.qml. This is because the qmldir file is in the same directory as app.qml: this is equivalent to having PieChart.qml and PieSlice.qml files inside the project directory, which could both be used by app.qml without import statements.
...全文
70 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

16,202

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧