Return qml component from c++ function
-
Hello,
I have a C++ class which acts as a factory, creating two different classes which implement the same interface.
I already registered that factory as a singleton via
QML_ELEMENT QML_SINGLETON
which works perfectly. Now I want to call mycreate
function , which returns the correct implementation, from qml.Is this possible? If so, how?
-
Hello,
I have a C++ class which acts as a factory, creating two different classes which implement the same interface.
I already registered that factory as a singleton via
QML_ELEMENT QML_SINGLETON
which works perfectly. Now I want to call mycreate
function , which returns the correct implementation, from qml.Is this possible? If so, how?
-
Hello,
I have a C++ class which acts as a factory, creating two different classes which implement the same interface.
I already registered that factory as a singleton via
QML_ELEMENT QML_SINGLETON
which works perfectly. Now I want to call mycreate
function , which returns the correct implementation, from qml.Is this possible? If so, how?
@Redman make the
create
functionQ_INVOKABLE
: Exposing Methods (Including Qt Slots) -
@Redman make the
create
functionQ_INVOKABLE
: Exposing Methods (Including Qt Slots)@GrecKo
Socreate()
is supposed to return instances of other C++ classes, that are exposed to QML, just not as singletons?I assume that you are familiar with this, since you have already succeeded with your singleton.
You need to expose the other classes just like the factory. As @GrecKo said, you implement a
Q_INVOKABLE NewClass *Factory::create(MaybeAnEnum type) { switch (type) { case Redman: return new NewClassRedMan; case GrecKo: return new NewClassGrecKo; case Axel: return new NewClassAxel; } }
To use it in QML, define a property of the appropriate type and assign the return value of
create()
.Rectangle { id: window property QtObject myClass: mySingleton.create(Redman) }
-
Hello,
I have a C++ class which acts as a factory, creating two different classes which implement the same interface.
I already registered that factory as a singleton via
QML_ELEMENT QML_SINGLETON
which works perfectly. Now I want to call mycreate
function , which returns the correct implementation, from qml.Is this possible? If so, how?