Ok, I understand the Q_property syntax and interaction better, but I am still having a bit of trouble understanding the access to the temperature property. I have been reading lots of examples and documentation on this, but it is all just becoming muddled. I think I am misunderstanding either Q_Property or the use of setContextProperty.
Right now, I create the temperature property with read and write access in a Q_Property:
@
class carData : public QObject {
Q_PROPERTY(int temperature READ temperature WRITE setTemperature NOTIFY temperatureChanged)
@
Then in main.cpp I create a carData object, and pass it into the setContextProperty as I have seen done in several examples.
@
MainWindow w;
carData *data = new carData();
w.rootContext()->setContextProperty("data", data);
@
I am confused by this part because the documentation has examples where objects are passed in and then used to access data (see background color example: http://apidocs.meego.com/1.2/qt4/qtbinding.html ), and also where properties within the QML are set such as in:
@
C++ { w.rootContext()->setContextProperty("information", 3);}
QML{ text: information }
@
I want to set temperature with all the property's read/write abilities, but I am not sure exactly how I should do this. Initially, I just had it set in the QML like this:
@
text: "Temperature: " + data.temperature
@
But, this returns undefined. Seeing that setContextProperty is setting a property, I also thought to try in QML something like this:
@
property int temperature: data.temperature
@
...but it simply sets temperature to 0, even when I have the return value statically defined as "2" etc, so I assume it is just setting 0 as the default value for temperature since data.temperature is not defined.
So, I am wondering, how do I correctly pass in the data object and access the temperature property in QML?
EDIT:
Problem Solved.
The problem was that by passing in the context as ("data", data), when referencing data in QML it wasn't correctly identifying the property. By changing what was passed in to ("carData", data), and setting property int temperature: carData.temperature, it works