Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. My custom C++ plugin is not being consumed by the qml test application
Forum Updated to NodeBB v4.3 + New Features

My custom C++ plugin is not being consumed by the qml test application

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 148 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by
    #1

    Hi Team,

    I am new to Qt and learning few of the modules in Qt-Qml from you tube and Qt documentation.

    I want to create cpp plugin which is to be consumed by the qml application.

    For this, I have created two projects. One is for creating Plugin library and another application is for consuming the plugin.

    I am able to create the plugin library successfully. But, not able to consume it by the Test application.

    The test application is compiled successfully but throwing below error during run time.
    QQmlApplicationEngine failed to load component
    qrc:/main.qml:3:1: module "MyPlugin" is not installed

    I have read the Qt documentation and googled a lot, but could not able to resolve it.

    Qt : 5.15.7
    Qt Creator: 5.0.3
    OS: Ubuntu 18.04

    Can some one please provide suggestions
    Thanks in advance for your help

    Project 1: Creating Plugin library
    CreatePlugin.pro

    TEMPLATE = lib
    TARGET = CreatePlugin
    QT += qml quick
    CONFIG += plugin c++11
    
    DESTDIR = ../pluginFiles
    TARGET = $$qtLibraryTarget($$TARGET)
    uri = MyPlugin
    
    # Input
    SOURCES += \
            createplugin_plugin.cpp \
            myplugin.cpp
    
    HEADERS += \
            createplugin_plugin.h \
            myplugin.h
    
    DISTFILES = 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
    }
    
    # Copy the qmldir file to the same folder as the plugin binary
    cpqmldir.files = qmldir
    cpqmldir.path = $$DESTDIR
    COPIES += cpqmldir
    

    createplugin_plugin.h

    #ifndef CREATEPLUGIN_PLUGIN_H
    #define CREATEPLUGIN_PLUGIN_H
    
    #include <QQmlExtensionPlugin>
    
    class CreatePluginPlugin : public QQmlExtensionPlugin
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
    
    public:
        void registerTypes(const char *uri) override;
    };
    
    #endif // CREATEPLUGIN_PLUGIN_H
    

    createplugin_plugin.cpp

    #include "createplugin_plugin.h"
    
    #include "myplugin.h"
    
    #include <qqml.h>
    
    void CreatePluginPlugin::registerTypes(const char *uri)
    {
        // @uri MyPlugin
        qmlRegisterType<MyPlugin>(uri, 1, 0, "MyPlugin");
    }
    

    myplugin.h

    #ifndef MYPLUGIN_H
    #define MYPLUGIN_H
    
    #include <QObject>
    #include <QString>
    
    class MyPlugin : public QObject
    {
        Q_OBJECT
        Q_DISABLE_COPY(MyPlugin)
    
    public:
        explicit MyPlugin(QObject *parent = nullptr);
        ~MyPlugin() override;
    
    public slots:
        QString getString();
    };
    
    #endif // MYPLUGIN_H
    

    myplugin.cpp

    #include "myplugin.h"
    
    MyPlugin::MyPlugin(QObject *parent)
        : QObject(parent)
    {
        // By default, QQuickItem does not draw anything. If you subclass
        // QQuickItem to create a visual item, you will need to uncomment the
        // following line and re-implement updatePaintNode()
    
        // setFlag(ItemHasContents, true);
    }
    
    MyPlugin::~MyPlugin()
    {
    }
    
    QString MyPlugin::getString()
    {
        return "This is CPP String";
    }
    

    qmldir

    module MyPlugin
    plugin CreatePlugin
    

    Project 2: TestPlugin
    TestPlugin.pro

    QT += quick
    
    CONFIG += c++11
    
    # You can make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
            main.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    QML_DESIGNER_IMPORT_PATH =
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import MyPlugin 1.0 
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        MyPlugin {
            id: mypluginId
        }
    
        Component.onCompleted: console.log(mypluginId.getString())
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved