How to use property change signal if used with setContextProperty()?
-
Hi everyone,
I have problems writing my first qml/c++ app. Maybe someone can lend me a helping hand?
My app should show some health states including network settings. Reading the "ipAddress" is no problem, but I get a parser error on "eth0.onIpAddressChanged", saying the eth0 is unknown. Couldn't be that much, but I havn't found the problem/a solution yet.
Thanks in advance,
Horstcpp:
@class netData : public QObject {
Q_OBJECT
Q_PROPERTY(QString ipAddress READ getIpAddress WRITE setIpAddress NOTIFY ipAddressChanged)QString getIpAddress(void) const;
void setIpAddress(QString address);signals:
void ipAddressChanged(QString addr);
}@main.cpp:
@...
context->setContextProperty("eth0", myNetData );@qml:
@Rectangle {
width: 360
height: 360Text { color: "#000000" height: 20 x: 10 y: 10 id: text1 text: "ipAddr: " + eth0.ipAddress font.weight: Font.Bold font.pixelSize: 11 verticalAlignment: Text.AlignVCenter } eth0.onIpAddressChanged: { text1.text = "ipAddr: " + addr; }
}@
-
You need to use that block (JS connect style) in a JavaScript block, for example in Component.onCompleted:
@
Rectangle {
// ...function addressChange() {
text1.text = "ipAddr: " + eth0.ipAddress;
}Component.onCompleted: {
eth0.ipAddressChanged.connect(addressChange);
}
@