Qt Quick 2 Extension Plugin 扩展插件

一.扩展插件的使用

在我们使用Qml的时候都会看到类似下面的语句

import QtQuick 2.2
import QtQuick.Window 2.2
import QtQuick.Controls 1.2

通过上面的import语句,,我们就可以使用qml中对应的控件了

模块的导入语句import + 模块名称 + 版本号

Qt本身就有很多已经封装好的模块,我们可以直接拿来使用.如下图所示


二.扩展插件的创建

其实扩展插件的制作是比较简单的,跟着下面的图片一步一步走就OK了.

新建文件或项目 -> Library -> Qt Quick 2 Extension Plugin,之后一步一步点下去就ok了.


下面是我随便写的两个模块,后面就是用我写的模块进行测试,一个MyRect.qml,一个MyStyle.qml.这个两个文件中有个差异是,MyStyle.qml里面增加了pragma Singleton.之后再qmldir中声明模块的时候也存在一个singleton.大家应该可以看得懂是什么意思,从文字上看去应该是单例,其实实际上也是单例的意思.这个好像扯得有点远了.






到这里,扩展插件就编写完成了,之后我们就可以进行编译生成了,在debug还有release会生成MyPlugind.dll和MyPlugin.dll两个库.

三.使用外部插件

自己已经写完了一个插件,那么就要去使用它,否则写出来就没有什么意义了.那么要怎么去使用自己创建的外部插件呢?  

1.首先我们要在自己的项目路径(也就是有.pro文件的那个目录)下面创建一个文件夹,我这里将其取名为plugin.之后将我们刚才的那个外部插件目录拷进去.如下图所示(我的项目是untitled),文件中需要包括生成的2个dll,qmldir文件,以及MyRect.qml和MyStyle.qml文件



2.需要在.pro文件中加入这样一段,意思就是将一个模块导入到一个项目中,当前目录下面的plugin目录,这个也是我刚才新建的那层目录.这样做的目的是为了在使用的时候

import MyPlugin 1.0

这个语句下面不会出现红色的线,要知道.作为一个程序员,看到一个语句下面有红色的线,从心理上来看感觉很可怕.即使没有问题


3.好吧,我们开始自己编写的插件了,代码如下所示

ApplicationWindow {
    id:root
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle{
        anchors.fill: parent
        color: MyStyle.myColor
    }
    MyRect{
        anchors.centerIn: parent
    }
}

编译你会发现这样一个错误

module "MyPlugin" is not install,这个错误是说明没有找到插件,没关系,我们有很多手段让你可以找到它

(1).在main.cpp中增加如下代码

    QQmlApplicationEngine engine;
    engine.addImportPath("D:/workspace/Qt/untitled/plugin");

(2).设置环境变量,变量名可不能搞错了.


(3).通过Qt内置函数设置环境变量,这句话最好放在main函数开始的时候

qputenv("QML2_IMPORT_PATH", "D:/workspace/Qt/untitled/plugin");


4.好了,效果图如下外面的大的,Rectangle的颜色是自己定义的单例的QtObject对象中的颜色赋予的,中间的Rectangle是自己定义的MyRect.qml控件.没毛病



四.对于扩展插件的相关疑问

1.MyPlugin文件中的Name.qmltypes文件是什么.

答:这个是插件信息文件,可以让Qt Creator争取读取typeinfo信息并且进行补全.

这个是.qmltypes文件生成命令

qmlplugindump -nonrelocatable MyPlugin 1.0 /import/path > /import/path/my/module/plugins.qmltypes


2.MyStyle.qmlc文件是什么文件

答:这个文件大家不用去管,就是增加编译器编译速度的.


阅读更多

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

11-11

我打开了我安装qt后的这个例子, extending中有6个例子,前5个可以编译运行. rn这个是第6个例子:rnC:\Qt\2010.05\qt\examples\declarative\tutorials\extending\chapter6-pluginsrnrn现在我构建了这个例子,生成了一个lib文件夹,里面有chapter6-plugins.dll和chapter6-plugins.a两个文件.rn但是我再cmd下却换到C:\Qt\2010.05\qt\examples\declarative\tutorials\extending\chapter6-plugins这个目录,然后用qmlviewer app.xml 运行,却没有任何反应.... rn谁能告诉我改怎么使用这个dll.或者说完成这个例子.rnrnrn附原文章:rnChapter 6: Writing an Extension PluginrnrnFiles:rnrndeclarative/tutorials/extending/chapter6-plugins/app.qmlrndeclarative/tutorials/extending/chapter6-plugins/chartsplugin.cpprndeclarative/tutorials/extending/chapter6-plugins/chartsplugin.hrndeclarative/tutorials/extending/chapter6-plugins/piechart.cpprndeclarative/tutorials/extending/chapter6-plugins/piechart.hrndeclarative/tutorials/extending/chapter6-plugins/pieslice.cpprndeclarative/tutorials/extending/chapter6-plugins/pieslice.hrndeclarative/tutorials/extending/chapter6-plugins/chapter6-plugins.prorndeclarative/tutorials/extending/chapter6-plugins/qmldirrnCurrently 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.rnrnTo create a plugin library, we need:rnrnA plugin class that registers our QML typesrnA project file that describes the pluginrnA qmldir file that tells the QML engine to load the pluginrnFirst, 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.rnrnHere is the ChartsPlugin definition in chartsplugin.h:rnrn #include rnrn class ChartsPlugin : public QDeclarativeExtensionPluginrn rn Q_OBJECTrn public:rn void registerTypes(const char *uri);rn ;rnAnd its implementation in chartsplugin.cpp:rnrn #include "piechart.h"rn #include "pieslice.h"rn #include rnrn void ChartsPlugin::registerTypes(const char *uri)rn rn qmlRegisterType(uri, 1, 0, "PieChart");rn qmlRegisterType(uri, 1, 0, "PieSlice");rn rnrn Q_EXPORT_PLUGIN2(chartsplugin, ChartsPlugin);rnThen, 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:rnrn TEMPLATE = librn CONFIG += qt pluginrn QT += declarativernrn DESTDIR = librn OBJECTS_DIR = tmprn MOC_DIR = tmprnrn HEADERS += piechart.h \rn pieslice.h \rn chartsplugin.hrnrn SOURCES += piechart.cpp \rn pieslice.cpp \rn chartsplugin.cpprnrn symbian rn include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri)rn TARGET.EPOCALLOWDLLDATA = 1rn rnFinally, 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:rnrn plugin chapter6-plugins librnNow 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:rnrn qmlviewer app.qmlrn(On Mac OS X, you can launch the "QMLViewer" application instead.)rnrnNotice 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.

没有更多推荐了,返回首页