Question

If i have type registered

qmlRegisterType<MyClass>("com.sample.myclass", 1, 0, "myClass");

and i want to get pointer to registered type i.e,

MyClass* registeredClass = **getQmlRegisteredType<MyClass>(1, 0)**;

Is it even possible? Reason why i want it because i want to set some properties of same object at certain time so that when qml looks at it, it knows. The other reason is i want to set context property so that i do not have to do:

MyClass {
    id: cls
}
....
cls.someMethod

I will just do

((QQmlContext*)viewer.rootContext())->setContextProperty("cls", 
    (QObject*)pointerToMyClass); // Pardon my C style cast

Thanks

Was it helpful?

Solution

I would personally not declare the class in QML directory. I would expose an object instance from C++ via the context propety system, and then you could also have access to that object on the C++ side, no matter what you wish to do with it.

This is some pseudo-code that I would write if I were you:

MyClass myClass;
QQmlContext *ctxt = view->rootContext();
ctxt->setContextProperty("myClass", &myClass);

Note that all the casts you are doing is unnecessary. Having this done, you could have access to the "myClass" instance. You could pass it on to other parts of the code base if needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top