3
votes

I have a QML file that contains some reusable application-wide data:

CApplicationData.qml:

pragma Singleton
import QtQuick 2.6
/**
 *  Singleton object to store all application data to be shared between different .qml files.
 *  These objects are received from the cloud or they can be application specific global data.
 */
QtObject {
  ...
  property var userObject

}

I've defined qmldir file like so:

qmldir

singleton ApplicationData 1.0 CApplicationData.qml

And imported it in each of the QML files where I want to reference it like so:

CUserEditView.qml:

import QtQuick 2.6
import "." // QTBUG-34418, singletons require explicit import to load qmldir file

Component.onCompleted {
    Console.log('ApplicationData: '+ApplicationData.userObject)
}

This works fine (most places), and when I debug on my development machine, QtCreator, I can see values defined inside of UserView.qml, after I display it (as a modal):

var component = Qt.createComponent("CUserEditView.qml");
var obj = component.createObject(rootWindow, {});

However, when I compile and use qtwindeploy, I have discovered that when I attempt to dereference ApplicationData.userObject, ApplicationData.userObject is undefined inside CUserEditView undefined?

Any ideas?

1
You have your QML in qrc or in a dir? - derM

1 Answers

1
votes

This was kind of strange behavior but I ultimately figured it out (Undocumented so far as I can tell)

I had to create a new QML sub-directory:

qml.qrc/applicationdata: /applicationdata/CApplicationData.qml /applicationdata/qmldir

with the contents of qmldir set to: singleton ApplicationData 1.0 CApplicationData.qml And add both files to my qml.qrc

This also required me to update my references in other .qml files to:

import "qrc:/applicationdata/."