Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML importing module

I want to import a custom module in my main.qml file. Main.qml is located under "/" prefix of my qml.qrc resource.

My custom module Config.qml is located within Config subdirectory. (Config directory is where main.qml is, i.e. /path/to/main/Config/Config.qml.

The config.qml and qmldir files are stored under the prefix myPrefix in the qml.qrc file.

Project
|- Config
    |- Config.qml
    |- qmldir
|- main.qml

Also I created a qmldir file which is according to the documentation http://doc.qt.io/qt-5/qtqml-modules-identifiedmodules.html necessary. Here are my Config.qml and qmldir files.

Config.qml

pragma Singleton
import QtQuick 2.0
QtObject {
    property int myVariable: 10
}

qmldir

singleton Config 1.0 Config.qml

When I want to import my custom module asMyModule in the main.qml file.

import "???" as MyModule

How can I do that? Does someone have a suggestion?

Edit:

qrc file

<RCC>
<qresource prefix="/">
    <file>main.qml</file>
</qresource>
<qresource prefix="/myPrefix">
<file>Config/qmldir</file>
<file>Config/Config.qml</file>
</qresource>

like image 582
OnurA Avatar asked Oct 19 '22 14:10

OnurA


2 Answers

** Question has been changed after the answer of Arpegius to raise another issue, I answer this new issue. **

This has nothing to do with qrc-prefix.
I believe your are mixing two different methods to import.


With or without prefix, to import a module you need to set the import-path of the QtQuick engine accordingly.

In your case, because your module directory is in the project-root directory :

engine.addImportPath("qrc:/");
// Now engine will look for subfolders which could be modules == with a qmldir

And in your main.qml you do the import using the prefix path instead of the filesystem path :

import myPrefix 1.0 as MyNamespace

You can also import simple QML files and not as a module :

// Because the path is relative to main.qml even in a qrc
import "myPrefix" as MyNamespace

Then you don't need the qmldir at all.

like image 107
ymoreau Avatar answered Nov 03 '22 08:11

ymoreau


From documentation:

The module's qmldir file must reside in a directory structure within the import path that reflects the URI dotted identifier string, where each dot (".") in the identifier reflects a sub-level in the directory tree. For example, the qmldir file of the module com.mycompany.mymodule must be located in the sub-path com/mycompany/mymodule/qmldir somewhere in the import path.

So you should change module MyModule to module Config or import it within specific path:

import "./Config" as MyModule
like image 39
Arpegius Avatar answered Nov 03 '22 08:11

Arpegius