Pragma singleton inside QML doesnt works for me
-
Hi
I'm trying to use new QML feature: pragma Sigleton
but when use the singleton item, Qt always claim: ReferenceError: <Item> is not defined
here is singletone item's code:
SysPref.qml
@pragma Singleton
import QtQuick 2.2
import QtQuick.Window 2.1QtObject {
property int itemSizeLarge: 16//Screen.logicalPixelDensity * 16
property int itemSizeMedium: 10//Screen.logicalPixelDensity * 10
property int itemSizeSmall: 8//Screen.logicalPixelDensity * 8
}@trying to access to it:
@console.log("item size large:", SysPref.itemSizeLarge)@
and got mentioned error
my bug ot Qt's or is it still not implemented?
Qt latest stable branch -
ok, digging git commits of qtdeclarative, I've found missed part:
Usage:
// First, define your QML singleton type which provides the functionality.
@pragma Singleton
import QtQuick 2.0
Item {
property int testProp1: 125
}
@
// Second, register the QML singleton type by calling this function in an initialization function.
@#include <QtQml>
...
qmlRegisterSingletonType(QUrl("file:///absolute/path/SingletonType.qml"), "Qt.example.qobjectSingleton", 1, 0, "RegisteredSingleton");
@
In order to use the registered singleton type in QML, you must import the singleton type.
@
import QtQuick 2.0
import Qt.example.qobjectSingleton 1.0
Item {
id: root
property int someValue: RegisteredSingleton.testProp1
}@ -
Hi, I tried your example,
for me it gives the following error during compilation.!bq. error: C2664: 'int qmlRegisterSingletonType(const char *,int,int,const char *,QJSValue (__cdecl *)(QQmlEngine *,QJSEngine *))' : cannot convert parameter 2 from 'const char [7]' to 'int'
There is no context in which this conversion is possibleam using Qt 5.1. please help.
-
Hi
Probably you have to use Qt 5.2 and later
-
Try the following. The order is unimportant.
- Create QML file (e.g., Test.qml) that uses singleton and add to your .qrc file
- Create MySingletonDir subdirectory
- Create singleton QML file (e.g., MySingleton.qml) in MySingletonDir
- Specify
@
pragma Singleton
@
at top of MySingleton.qml
5. Add qmldir file in same directory as MySingleton.qml with content
@
singleton MySingleton 1.0 MySingleton.qml
@- Add MySingleton.qml and qmldir to your .qrc file
- Add
@
import "MySingletonDir" 1.0
@
to Test.qml
8. Reference singleton in Test.qml (e.g., MySingleton.myProperty)Nothing is required in any C++ file.
-
Hi SteveG
I tried the same method you said. the singleton is working fine all the places except inside the states.
example
states:[
State
{
name: "HitState"PropertyChanges { target: gradientMainColor color: single.button_hitstate_color } PropertyChanges { target: gradientMainColor1 color: single.button_hitstate_color } ]
the error message is single undefined
-
Hi SteveG
Yes single is my singleton and button_hitstate_color is one of the property in MySingleton.qml
Inisde qmldir i have created the module names Styles with version 1.0
singleton single 1.0 MySingleton.qmlwhere ever i need this i include module import Styles 1.0
and start using the single.property ... it works fine for all the places in the any qml file except inisde the states i,e property changes -
Mahaboobsab,
As a possible workaround (i.e., hack), does it work if you define
@
property color my_button_hitstate_color: single.button_hitstate_color
@and reference my_button_hitstate_color in PropertyChanges?
Regardless,you may want to file a bug report. It would be helpful to attach zipped sample files with a singleton, a qmldir file and a QML component that shows the singleton not working in PropertyChanges but working other places.
-
Hi SteveG,
singleton single 1.0 MySingleton.qml
it worked by giving first letter of singleton name in Caps as "Single"
i am wondering how its related to name ..if it is qml file name it has to be in Caps but singleton name.
-
Hi Steveg
I am using singleton but facing problem if i use more than 2 singleton in one qmldir .
can u comment on this.?