QML importing module
Asked Answered
T

3

7

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>

Thundersquall answered 29/2, 2016 at 11:46 Comment(2)
Isn't the module folder suppose to have same name as the module?Foregoing
Also didn' work with import Config 1.0, when I changed to module ConfigThundersquall
W
4

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

Workshop answered 1/3, 2018 at 15:40 Comment(0)
F
2

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
Foregoing answered 29/2, 2016 at 13:32 Comment(1)
I think the problem is, I have a prefix for the Config directory in the qml.qrc file and the import statement should also include the qrc:/prefix/... somehow. But I can't figure out how. Can you write an example with a prefix before Config directory?Thundersquall
P
0

if this is still an issue for anyone, you to right click on the file and select add file to directory. then you select the directory you wish to add the qml file in. this directory should be the same as the main qml file. doing this solved my problem

Philadelphia answered 11/11, 2020 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.