I am new to QML and have a problem in accessing a property.property of a C++ object:
C++, frequency and station both are Qt metatype registered objects:
CStation *station = new CStation(...); // QObject
CFrequency *frequency = new CFrequency(..); // QObject
QQmlContext *qmlContext = viewer.rootContext();
qmlContext->setContextProperty("myatcstation", station);
qmlContext->setContextProperty("myfrequency", frequency);
QML:
RowLayout { ....
TextField {
text: myatcstation.toQString(true)
}
}
.... text: myfrequency.toQString(true)
This works, but when I write: text: myatcstation.frequency.toQString(true)
I do get TypeError: Object [object Object] has no method 'toQString'
frequency
is a property of class CStation
set as Q_PROPERTY(CFrequency frequency READ getFrequency)
Crosscheck in C++ works:
CFrequency test = station->property("frequency").value<CFrequency>();
-- Edit: Frank's answer --
Both classes are derived from QObject
, and it is not as per textbook as they are made copyable. I am aware of the Identity vs value situation.
Basically frequency is a value object, but I have made it QObject
based so I am able to use properties with it (see Any chance to use non QObject classes with QML ). In the example, toString
is Q_INVOKABLE
, frequency in the non-working case returns a copy of a QObject
derived CFrequency
object.
-- Edit: Further findings --
When I change the frequency property to return CFrequency*
instead of CFrequency
it does not work either. As I get TypeError: Cannot call method 'toQString' of undefined
the situation seems to be the same. CFrequency
alone works, but QML does not understand that myatcstation.frequency
is a frequency object which has toString
.