GrahamSim
GrahamSim

Reputation: 87

How to import QML singleton

I am trying to import my singleton module but keep getting module "Style" is not installed

This is my project structure

enter image description here

Style.qml

pragma Singleton
import QtQuick 2.0

QtObject {

    property color subsectionlabelColor: "white"
    property color appSectionTitleColor: "white"
}

qmldir

singleton Style 1.0 Style.qml

Main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.3

import Style 1.0

Window {

I wanted to keep the Style.qml outside of the qrc file so it can be changed more easily.

Please can someone explain what is going on?

Thanks

Upvotes: 1

Views: 3433

Answers (2)

seilena
seilena

Reputation: 31

Use addImportPath API of engine to add path of the directory where your qtdir file is located. Refer main.cpp file of the example https://doc.qt.io/qt-5/qtquickcontrols-flatstyle-example.html

The above mentioned API can be used in your main function as:

int main(int argc, char *argv[])

{

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.addImportPath(":/imports"); /* Insert relative path to your import directory here */
engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));

return app.exec();
}

Import paths needs to be added to solve the error "module not installed" . By default, the import paths would be that of QT source directory

Upvotes: 0

TrebledJ
TrebledJ

Reputation: 8987

Normally, it should work if you import the directory where your singleton is located.

In your Main.qml, replace import Style 1.0 with import "Style" (or whatever seems to be your relative path from Main.qml to the directory containing Style.qml. Importing the directory should run the qmldir script, which allows for the singleton to kick in.

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.3

import "Style"  // relative path to the directory containing Style.qml

Window {

Upvotes: 2

Related Questions