Use a c++ class as singleton inside qml files
-
What about the link below
"http://developer.qt.nokia.com/forums/viewthread/6201":http://developer.qt.nokia.com/forums/viewthread/6201
-
Wow.. interesting. I don't know how to do it. Let me find sometime to find out.
-
ok.. one thing you can do is to connect your MyClass's onSignal some signal in your qml code.
@QObject * root = viewer->rootObject();
QObject *qmlItem = obj->findChild<QObject *>("qmlItem "); // "qmlItem" is objectName of one of your qml items.
QObject::connect(&MyClass, SIGNAL(onSignal ()),qmlItem, SIGNAL(qmlItemSignal ()));@try it out and let us know if this works.
-
Hello Vijay,
Yes I kinda tested the "connect" thing and it worked, but it is not satisfying for me, as I may have multiple classes from which I will have to handle signals and link them to several different qml files, and I don't want to have all of them managed in my main.c file, at all. I would like everything to be handled in the QML part really.
By the way, do you have a solution to create a singleton from within QML only?
thank you,
Bill -
ok :). One more thought. Have your MyClass qml instance in your root class ( I mean main.qml rootitem), and have a javascript function which finds root item, and then gets your class id.
your javascript class something like this
@function getSingleton(var currentItem)
{
var parentItem = currentItem
while(parentItem) {
parentItem = parentItem.parent
}
return parentItem.mySingletonItem
}@Don't know if this works, but worth giving a try.
-
Hello Vijay, thank you for your new suggestion.
Where do I put this javascript function, and how do I declare an instance of MyClass that would be the return value of this function? I didn't understand, sorry.
Bill -
I will try this out and let you know if this work. I should post the complete code example in case if it works.
-
I'm trying to do it on my side in the mean time too, and try to make it make some sense to me ;)
Thank you Vijay, I stay tuned!
Bill -
Got an answer from guyz @ Nokia telling me it was not a good idea in the first place, and that the singleton implementation should remain withinj the C++ code. So I think that settles it!
-
oho ok.. but whey is it not a good idea to have a singleton class in QML. Does it mean that we are not advised to have business logic in QML and move it to c++. And mostly we don't want to have singleton class for a displayable item and keeps moving it around??
-
Here is their whole answer
"I have been thinking about this for a while, but didn't come up with a good solution. Besides, it feels wrong somehow trying to apply this OOP pattern to a declarative language like QML. There might be some tweaks in Javascript, but I would rather suggest to solve this in C++ or take a different approach. I mean even, if you would implement your declarative item as a singleton, the QML engine couldn't instantiate it, as it needs a public constructor."
Regards,
Bill -
I know it's a old question but, did you tried to connect to the exported property using a Connections item ?
@
Connections {
target: objectQml
onSignal: //dosomething
}
@