Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Registering .pragma singleton libraries in custom plugins



  • Hello :)

    I'm trying to change a way I register .js libraries in my custom QML plugins.
    Till now, when I wanted to make a simple .js based library, I included a path to it in my qmldir file. For example for plugin named IzLibrary I have:

    module IzLibrary
    plugin IzLibraryPlugin
    typeinfo izlibrary.qmltypes
    classname IzQMLComponents
    IzToast 1.0 qrc:///src/JS/IzToast.js
    IzMark 1.0 qrc:///src/JS/IzMark.js
    IzBusy 1.0 qrc:///src/JS/IzBusy.js
    

    Where IzMark is just:

    .pragma library
    .import QtQml 2.0 as Qml
    
    function mark(target, color) {
    	var component = Qt.createComponent("qrc:/src/QML/IzMark.qml");
    	if (component.status === Qml.Component.Ready) {
    		finishCreation(component, target, color);
    	} else {
    		component.statusChanged.connect(finishCreation(component, target, color));
    	}
    }
    
    function finishCreation(component, target, color) {
    	if (component.status === Qml.Component.Ready) {
    		var object = component.createObject(target,
    		{
    			"target": target,
    			"border.color": color
    		});
    		if (object === null) {
    			console.log("Error creating mark.");
    		}
    	} else if (component.status === Qml.Component.Error) {
    		console.log("Error loading mark:", component.errorString());
    	}
    }
    
    

    What I would like to do, is to register it like I do with .qml files in my QQmlExtensionPlugin derived classes:

    qmlRegisterType(QUrl(QStringLiteral("qrc:/src/QML/IzModalBlur.qml")), "IzLibrary", 1, 0, "IzModalBlur");
    

    Is something like that possible? I have found Simon Hausmann comment in this issue which mentions .js file and regular qmlRegisterType() function but when I do this:

    qmlRegisterType(QUrl(QStringLiteral("qrc:/src/JS/IzMark.qml")), "IzLibrary", 1, 0, "IzMark");
    

    and later call it like this:

    IzMark.mark(object, Material.color(Material.Orange));
    

    all I get is:

    WARNING   | qrc:/src/QML/Pages/TestPage.qml[49] | QML: TypeError: Property 'mark' of object [object Object] is not a function
    


  • I can't edit my post due to some Akismet.com shenanigans but of course I meant:

    qmlRegisterType(QUrl(QStringLiteral("qrc:/src/JS/IzMark.js")), "IzLibrary", 1, 0, "IzMark");
    

Log in to reply