How to get network status in QML?

I wrote an applet that makes an API call every 10 minutes, but I want it to show a message if the computer is offline. This means I need to get the network status within QML. I thought a DataEngine might be available, but there isn’t.

I read the code for plasma-nm and found this solution hack:


<span style="color:#323232;">import org.kde.plasma.networkmanagement 0.2 as PlasmaNM
</span><span style="color:#323232;">PlasmaNM.NetworkStatus {
</span><span style="color:#323232;">    id: networkStatus
</span><span style="color:#323232;">}
</span><span style="color:#323232;">PlasmaComponents3.Label {
</span><span style="color:#323232;">    visible: networkStatus.networkStatus != "Connected"
</span><span style="color:#323232;">    text: i18n("Network disconnected")
</span><span style="color:#323232;">}
</span>

But I think comparing strings is a terrible way because I don’t know what will happen in other locales.

Is there a better way?

Link to my code

carlschwan Mod ,

Did you try:


<span style="color:#323232;">import org.kde.plasma.networkmanagement 0.2 as PlasmaNM
</span><span style="color:#323232;">
</span><span style="color:#323232;">PlasmaNM.NetworkStatus {
</span><span style="color:#323232;">    id: networkStatus
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">PlasmaComponents3.Label {
</span><span style="color:#323232;">    visible: networkStatus.connectivity !== PlasmaNM.NetworkManager.Full
</span><span style="color:#323232;">    text: i18n("Network disconnected")
</span><span style="color:#323232;">}
</span>

This use an enum and should be a lot more robust

fkfd OP ,
@fkfd@lemmy.ml avatar

It seems networkStatus doesn’t provide a connectivity attribute and PlasmaNM.NetworkManager doesn’t have a Full either:


<span style="color:#323232;">console.log(Object.keys(networkStatus))
</span><span style="color:#323232;">qml: [objectName,activeConnections,networkStatus,objectNameChanged,activeConnectionsChanged,networkStatusChanged]
</span>

<span style="color:#323232;">networkStatus.connectivity !== PlasmaNM.NetworkManager.Full
</span><span style="color:#323232;">.../contents/ui/main.qml:150: TypeError: Cannot read property 'Full' of undefined
</span>
Brochetudo ,

You should list the properties of the object you are dealing with. Perhaps there are some unknown functions/properties that may do what you want

lecris ,

What about checking via dbus?

fkfd OP ,
@fkfd@lemmy.ml avatar

that would be ideal! but I can’t seem to find a way to do it natively without some C++ library like this one?

lecris ,

I need to setup notifications here. I am not familiar with how qml works, but can’t ituse qt native integration, like the one documented in the kde page

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • [email protected]
  • All magazines