How create shared library in QT/QML

雨燕双飞 提交于 2019-11-28 05:55:31

问题


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 that.


回答1:


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



回答2:


Have a look at the documentation for QML Modules

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



来源:https://stackoverflow.com/questions/40968813/how-create-shared-library-in-qt-qml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!