1、使用项目创建工具,创建QtQuick2 Plugin工程。
2、有两个名字很重要。
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;"><span style=" color:#008000;">//</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">@uri</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">MyPlugin</span></span>
<span style="font-size:18px;">qmlRegisterType<<strong>ComManager</strong>>(<strong>uri</strong>, 1, 0, "<strong>ComManager</strong>");</span>
QML中的写法是 import MyPlugin 1.0,然后在代码里面写 ComManager{...}。
3、生成dll文件,和qmldir文件。qmldir文件内容如下:
module MyPlugin
plugin ComManager
两个文件放在一个新建的文件夹下,文件夹名字为MyPlugin
4、调试代码时,把MyPlugin文件夹放在exe文件同级目录下,QML才能install plugin。因为是动态调用嘛~~