无法导入QML插件

时间:2014-07-30 05:38:31

标签: qt qml

让我们考虑在目录$HOME/projects/org/example/PluginExample中创建的示例QML插件 PluginExample (请参阅C++ Plugins for QML)($HOME/projectsQML2_IMPORT_PATH中)。< / p>

pluginexample.pro

TEMPLATE = lib
TARGET = PluginExample
QT += qml quick
CONFIG += qt plugin

TARGET = $$qtLibraryTarget($$TARGET)
uri = org.example.PluginExample

# Input
SOURCES += \
    pluginexample_plugin.cc \
    myitem.cc

HEADERS += \
    pluginexample_plugin.hh \
    myitem.hh

OTHER_FILES = qmldir

!equals(_PRO_FILE_PWD_, $$OUT_PWD) {
    copy_qmldir.target = $$OUT_PWD/qmldir
    copy_qmldir.depends = $$_PRO_FILE_PWD_/qmldir
    copy_qmldir.commands = $(COPY_FILE) \"$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)\"
    QMAKE_EXTRA_TARGETS += copy_qmldir
    PRE_TARGETDEPS += $$copy_qmldir.target
}

qmldir.files = qmldir
unix {
    installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /)
    qmldir.path = $$installPath
    target.path = $$installPath
    INSTALLS += target qmldir
}

myitem.hh

#ifndef MYITEM_H
#define MYITEM_H

#include <QQuickItem>

class MyItem : public QQuickItem
{
    Q_OBJECT
    Q_DISABLE_COPY(MyItem)
    Q_PROPERTY(QString text READ text CONSTANT)

public:
    MyItem(QQuickItem *parent = 0);
    QString text() const;
    ~MyItem();
};

#endif // MYITEM_H

myitem.cc

#include "myitem.hh"

MyItem::MyItem(QQuickItem *parent):
    QQuickItem(parent)
{
}

MyItem::~MyItem()
{
}

QString MyItem::text() const
{
    return "MyItem";
}

pluginexample_plugin.hh

#ifndef PLUGINEXAMPLE_PLUGIN_H
#define PLUGINEXAMPLE_PLUGIN_H

#include <QQmlExtensionPlugin>

class PluginExamplePlugin : public QQmlExtensionPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")

public:
    void registerTypes(const char *uri);
};

#endif // PLUGINEXAMPLE_PLUGIN_H

pluginexample_plugin.cc

#include "pluginexample_plugin.hh"
#include "myitem.hh"

#include <qqml.h>

void PluginExamplePlugin::registerTypes(const char *uri)
{
    // @uri org.example.PluginExample
    qmlRegisterType<MyItem>(uri, 1, 0, "MyItem");
}

qmldir

module org.example.PluginExample
plugin PluginExample

插件代码的大部分内容都是由项目向导自动生成的。

它构建良好,但当我尝试从另一个项目import org.example.PluginExample 1.0时,我收到此错误:

module "org.example.PluginExample" plugin "PluginExample" not found

我该如何解决?我是否需要指定其他信息来构建/定位插件?

PS。 Qt 5.3,Linux x86_64


编辑1

我将libPluginExample.so从构建目录(build-PluginExample-Desktop_Qt_5_3_0_GCC_64bit-Debug)复制到源目录(PluginExample),错误消失了。这是最好的解决方案吗?

编辑2

为避免在每次构建后手动复制插件库,需要对 * .pro 文件(特定于unix)应用以下更改:

!equals(_PRO_FILE_PWD_, $$OUT_PWD) {
    # ...

    unix {
        QMAKE_POST_LINK += $(COPY_FILE) $$quote($${OUT_PWD}/lib$${TARGET}.so) $$_PRO_FILE_PWD_
    }
}

0 个答案:

没有答案