0
votes

I created a project that puts some QML in a plugin, and an app using Qt's QPluginLoader.

The app's QML can then use the plugin's QML, with one noticeable problem. From one of the plugin's QML files, I am not able to read a property of a singleton in another QML file. If you look in the file hello.qml, there is a Text item that tries to get his text from MySingleton, but that text does not show up. When I substitute a string literal (the commented out line), the text shows up just fine.

I've got the project here: https://github.com/rhvonlehe/qmlplugin

The project itself is very basic, but represents a lot of the same things going on in a larger project that I can't share but has the same problem.

Output as-is:

enter image description here

In the blue rectangle there should be the words, "singleton Text"

1

1 Answers

0
votes

The QtCompany provided the answer, which I'm able to share here.

Long story short, to get the full functionality expected from QML in a plugin I needed to use QQmlExtensionPlugin. Here are the updated files to make things work (Plugin.h and hello.qml)

hello.qml

import QtQuick 2.0
import org.example.PluginInterface 1.0
import "."

Rectangle {
    id: page
    width: 320; height: 240
    color: "lightgray"

    Text {
        id: helloText
        text: "Hello world!"
        y: 30
        anchors.horizontalCenter: page.horizontalCenter
        font.pointSize: 24; font.bold: true
    }

    Rectangle {
        id: innerPage
        width: 300; height: 30
        anchors.horizontalCenter: page.horizontalCenter
        color: "blue"

        Text {
            id: nextOne
            color: "white"
//            text: "literal Text"
            text: MySingleton.getTheText()
            anchors.verticalCenter: innerPage.verticalCenter
            anchors.horizontalCenter: innerPage.horizontalCenter
            font.pointSize: 18; font.bold: false
        }
    }

    OtherThing {
        id: myOtherThing
        x: 20; y: 150
    }

}

Plugin.h

#ifndef PLUGIN_H
#define PLUGIN_H

#include <PluginIf.h>
#include <QQmlExtensionPlugin>
#include <QQmlEngine>

class Q_DECL_EXPORT Plugin : public QQmlExtensionPlugin, PluginInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
    Q_INTERFACES(PluginInterface)
public:
    void init() override;
    void registerTypes(const char *uri) override
    {
        qmlRegisterSingletonType(QUrl("qrc:/MySingleton.qml"), uri, 1, 0, "MySingleton");
    }
};

#endif // PLUGIN_H