Hello,
I’m trying to implicitly convert an object to QString in order to
assign it to a Text element in qml.
I think an example makes things clear:
class Bar : public QObject
{
...
Q_INVOKABLE QString value(int param = -1);
};
class Foo : public QObject
{
...
Q_PROPERTY(Bar * bar READ bar WRITE setbar NOTIFY onBarChanged);
};
A Foo object is registered as context property at application start:
viewer.rootContext()->setContextProperty("Foo", &fooObject);
In qml the following works perfectly:
Text
{
...
text: Foo.bar.value()
}
But in most cases I do not need to give a parameter to the value call() and it would
be much more straight forward if I could just write
Text
{
...
text: Foo.bar
}
At the moment there’s a runtime error which says “Unable to assign Bar to QString”.
I tried to add a type cast operator to Bar:
Q_INVOKABLE operator QString()
{return value();}
But this did not resolve the problem.
Is it possible to convert an object to a QString implicitly? And if yes: how?
Thank you very much in advance,
Martin
↧