How create shared library in QT/QML

后端 未结 2 1313
梦如初夏
梦如初夏 2020-12-20 08:21

I have 4 qml files and one main.cpp to load qml file. Is it possible for me to create 1 dll file for those 4 qml file. And use it in different application if so how to do th

相关标签:
2条回答
  • 2020-12-20 08:56

    As already said, there is no need for embedding qml files only in a library. But of course you have the right to do all you want, even that. I know at least 2 ways to do that:

    1. Create binary resource file
    Prepare resource file containing qml files and then compile it:

    rcc -binary plugin.qrc -o plugin.rcc
    

    Now you can include this file into your application :

    QResource::registerResource("plugin.rcc");
    

    and use it as regular qrc file:

    QResource::registerResource(qApp->applicationDirPath() + "/plugin.rcc");
    QQuickView *view = new QQuickView();
    view->setSource(QUrl("qrc:/qml/myfile.qml"));
    

    Here qml/ is prefix in resource file.

    2. Shared library
    Another way is to create a shared library containing the same resource file. For example your plugin's shared library implements following interface:

    interface.h

    #ifndef PLUGIN_INTERFACE_H
    #define PLUGIN_INTERFACE_H
    
    #include <QString>
    #include <QObject>
    
    class PluginInterface
    {
    public:
        virtual ~PluginInterface() {}
        virtual QByteArray getQML(const QString &name) = 0;
    };
    
    #define PluginInterface_iid "org.qt-project.PluginInterface"
    
    Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
    
    #endif
    

    and its implementation is:

    QByteArray PluginImpl::getQML(const QString &name)
    {
        QFile file(":/qml/" + name);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return QByteArray();
        return file.readAll();
    }
    

    Now, in your application you load your plugin and get its resource as a string:

    QDir pluginsDir(qApp->applicationDirPath());
    QPluginLoader pluginLoader(pluginsDir.absoluteFilePath("plugin.dll"));
    QObject *plugin = pluginLoader.instance();
    if (plugin) {
        PluginInterface *pluginInstance = qobject_cast<PluginInterface *>(plugin);
        if (pluginInstance) {
            QByteArray content = pluginInstance->getQML("file1.qml");
            QQuickView *view = new QQuickView();
            QQmlComponent component(view->engine());
            component.setData(content, QUrl());
            QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
            childItem->setParentItem(view->contentItem());
    
            QWidget *container = QWidget::createWindowContainer(view);
            container->setFocusPolicy(Qt::TabFocus);
            ui->verticalLayout->addWidget(container);
        }
    }
    

    But pay attention, when you deploy your application you anyway have to copy all qml system files, like #QTPATH/qml/QtQml, #QTPATH/qml/QtQuick.2, #QTPATH/qml/QtQuick.2 etc.

    Links:

    • Resource compiler
    • Same theme
    • Plugin example
    0 讨论(0)
  • 2020-12-20 09:01

    Have a look at the documentation for QML Modules

    There are options for QML-only modules, C++ only and mixed mode.

    0 讨论(0)
提交回复
热议问题