My project uses Qt and QML.
I want to bind a property that can be multiple types of a class.
For example:
class base : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QVariant myVar read myVar NOTIFY myVarChanged)
QVariant myVar() {return var;}
signals:
void myVarChanged();
private:
QVariant var
}
class A : public QObject
{
Q_OBJECT
/*
similar with upper class
*/
QString myStr;
}
somewhere
{
QPointer<A> mine = new A;
base.myVar = QVariant::fromValue(mine)
}
Now I can have Base in QML.
When I call:
console.log(base.myVar)
console prints:
QVariant(QPoitner<A>(/*address*/)
But I can't get further. I want to get myStr by using base.myVar.myStr, but it is undefined. How do I solve this?
I can't use Q_GADGET because myStr changes.
I have to put class B or C in myVar sometimes, not only class A, so I have to use QVariant.
There's
QVariant::fromValueandqvariant_cast.If you want to cast to
QObject*as an intermediate step, you candynamic_castbefore converting it to aQVarianton the way out, and, on the way back, you useqvariant_castthenqobject_cast.Another solution you may want to consider is using
QJSValue.QJSValue QJSEngine::newQObject(QObject* object)basically creates a JavaScript object that wraps the given QObject with JavaScriptOwnership. i.e. the QML Engine's garbage collector will take care of cleanup of the object when it goes out of scope. You usually use this approach if you are newing to your QObject, but, you want QML Engine to take care of the remainder of the object life-cycle. If you were to receive aQJSValuefrom QML, you will have to callQString QJSValue::toObject()to recover the QObject and then you will have to do aqobject_cast.References: