Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to use property change signal if used with setContextProperty()?
Qt 6.11 is out! See what's new in the release blog

How to use property change signal if used with setContextProperty()?

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HorstJ
    wrote on last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Torgeir
        wrote on last edited by
        #3

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

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

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HorstJ
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Torgeir
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved