Hey, i am afraid i am super confused as to how Singleton types are supposed to work.
The documentation on the area is sparse at best and when there is some, it’s all saying something different or something plain wrong. There has been a few discussions on this board dealing with this, and i can’t seem to make sense of it.
First of, the easiest way to declare a QML Singleton from within QML should be:
//inside SomeSingleton.qml
pragma Singleton
import QtQuick 2.2
import QtQml 2.0
QtObject{property myInt: 10}
//Inside SomeRect.qml
Rectangle{width : SomeSingleton.myInt}
Now, this simply doesn’t work. – replacing pragma with .pragma, or using a qmldir to describe the module doesn’t help either way or any documented
ways. For reference:
http://qt-project.org/wiki/QmlStyling
http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterSingletonType
Also in new features page of 5.2 (http://qt-project.org/doc/qt-5/whatsnew52.html ) It States:
Added ability to define QML singletons with QML files – The existing documentation on Styling/Singletons however are from before 5.2 – which lead me to believe some change might have happened in the way they work/are supposed to work.
Looking at the forums, it’s clear other people have had problems with this too. One “Solution” i found was that, you have to register the SingletonType from c++ and then import it in QML, this however doesn;t work either (for me).
//In myQMLEngineLoader.cpp
void RegisterMySingleton()
{
qmlRegisterSingletonType(QUrl::fromLocalFile("SomeSingleton.qml"), "Singletons.SomeSingleton", 1, 0, "MySingleton");
}
This will not produce errors, but when referencing MySingleton in a QML doc (import Singletons.SomeSingleton 1.0 ), it will simply say Component not Ready and or that any of the fields are undefined. – note that making the URL incorrect on purpose also doesn’t produce errors.
I am at a loss as to how to approach styling/global data with QML. Stateless Libraries aren’t really that nice, and instancing a “style” component for every component also seems stupid. It’s especially disheartening when any info on styling with QML simply points to how nice Singletons are, while providing 0 docs on how to use them or how to register these (the info that does exist is obviously wrong).
Please help,
Rune
↧