最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

qml - How to use Qt Quick Plugin in Qt Quick Application? - Stack Overflow

programmeradmin4浏览0评论

Qt doesn't find my imported module

When i build a plugin using qmake and make command, it creates a directory called release, which contains the .dll file.

My question is how can i use this plugin in Qt Quick Application?

I have tried in the .pro file QML_IMPORT_PATH += C:/Programming/Qt/test2/release And in the main.qml file import myplugin 1.0. But when i try to run the application it doesn't find the imported plugin.

Lets say i have a simple plugin like this that just sends s signal that contains a message :

myitem.h

#ifndef MYITEM_H
#define MYITEM_H

#include <QObject>
#include <QString>
#include <QTimer>
#include <QDebug>

class MyItem : public QObject
{
    Q_OBJECT
public:
    explicit MyItem(QObject *parent = nullptr);
    ~MyItem();

signals:
    void mySignal(const QString &message);

public slots:
    void sendMessage();
};

#endif // MYITEM_H

myitem.cpp:

#include "myitem.h"

MyItem::MyItem(QObject *parent) : QObject(parent)
{
    QTimer::singleShot(1000, this, &MyItem::sendMessage);
}

MyItem::~MyItem() {}

void MyItem::sendMessage()
{
    qDebug() << "Sending signal with message: Hello from C++!";
    emit mySignal("Hello from C++!");
}

test2_plugin.h:

#ifndef TEST2_PLUGIN_H
#define TEST2_PLUGIN_H

#include <QQmlExtensionPlugin>

class Test2Plugin : public QQmlExtensionPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)

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

#endif // TEST2_PLUGIN_H

test2plugin.cpp:

#include "test2_plugin.h"

#include "myitem.h"

#include <qqml.h>

void Test2Plugin::registerTypes(const char *uri)
{
    // @uri myplugin
    qmlRegisterType<MyItem>(uri, 1, 0, "MyItem");
}

qmldir:

module myplugin

plugin test2

Qt doesn't find my imported module

When i build a plugin using qmake and make command, it creates a directory called release, which contains the .dll file.

My question is how can i use this plugin in Qt Quick Application?

I have tried in the .pro file QML_IMPORT_PATH += C:/Programming/Qt/test2/release And in the main.qml file import myplugin 1.0. But when i try to run the application it doesn't find the imported plugin.

Lets say i have a simple plugin like this that just sends s signal that contains a message :

myitem.h

#ifndef MYITEM_H
#define MYITEM_H

#include <QObject>
#include <QString>
#include <QTimer>
#include <QDebug>

class MyItem : public QObject
{
    Q_OBJECT
public:
    explicit MyItem(QObject *parent = nullptr);
    ~MyItem();

signals:
    void mySignal(const QString &message);

public slots:
    void sendMessage();
};

#endif // MYITEM_H

myitem.cpp:

#include "myitem.h"

MyItem::MyItem(QObject *parent) : QObject(parent)
{
    QTimer::singleShot(1000, this, &MyItem::sendMessage);
}

MyItem::~MyItem() {}

void MyItem::sendMessage()
{
    qDebug() << "Sending signal with message: Hello from C++!";
    emit mySignal("Hello from C++!");
}

test2_plugin.h:

#ifndef TEST2_PLUGIN_H
#define TEST2_PLUGIN_H

#include <QQmlExtensionPlugin>

class Test2Plugin : public QQmlExtensionPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)

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

#endif // TEST2_PLUGIN_H

test2plugin.cpp:

#include "test2_plugin.h"

#include "myitem.h"

#include <qqml.h>

void Test2Plugin::registerTypes(const char *uri)
{
    // @uri myplugin
    qmlRegisterType<MyItem>(uri, 1, 0, "MyItem");
}

qmldir:

module myplugin

plugin test2

Share Improve this question asked Mar 11 at 6:52 joonakaikkonenjoonakaikkonen 12 bronze badges 1
  • My first advice would be to change to cmake as your build system. Many cmake macros make using modules much easier. – Jürgen Lutz Commented Mar 11 at 10:45
Add a comment  | 

1 Answer 1

Reset to default 0

If you mean running as a stand alone application and not throught Qt Creator you have at least two solutions

You can copy each plugin/module folder next to the executable you want to run

Each module must be in separate folders and include qmldir and library and/or qml files

yourapplication
ModuleOne
|-qmldir
|-pluginone.so
|-ComponentOne.qml
|-ComponentTwo.qml
ModuleTwo
|-qmldir
|-plugintwo.so

You have also the possibility to put all plugins/modules folder in a dedicated directory and set both env variable QML_IMPORT_PATH and QML2_IMPORT_PATH to this directory before running the application

发布评论

评论列表(0)

  1. 暂无评论