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,
    Horst

    cpp:
    @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: 360

    Text {
        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;
    }
    

    }@


  • Moderators

    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);
    }
    @



  • What sierdzio said, or you may use a Connections element:

    @Connections {
    target: eth0
    onIpAddressChanged: text1.text = "ipAddr:" + addr
    }
    @



  • Thanks both of you!

    Is it possible to use the connection element like this:
    @
    Connections {
    target: eth0
    onIpAddressChanged: text1.text = "ipAddr:" + addr
    onIpSubnetChanged: text1.text = "Subnet:" + mask
    }@



  • yes, if the netData class has a signal ipSubnetChanged with parameter named mask, that should work nicely.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.