[SailfishDevel] Binding singleton values and network availablity

Thomas Tanghus thomas at tanghus.net
Fri Aug 30 12:22:19 UTC 2019


With kind and subtle help from Coderus, I found ways to monitor network
availability. To use it in different places of the project, I'm trying
to make a singleton which holds relevant variables and states.

That part actually works - almost.

The singleton module(/object/class?): ;)

Env.qml:

  pragma Singleton

  import QtQuick 2.6

  QtObject {
      property bool isOnline: network.isOnline
      onIsOnlineChanged: console.log('Env.isOnline:', isOnline)

      property var network: {
          var component = Qt.createComponent(Qt.resolvedUrl('Network.qml'));
          return component.createObject();
      }
  }

This instantiates:

Network.qml:

import QtQuick 2.6
import org.freedesktop.contextkit 1.0

  QtObject {
      id: network
      property bool isOnline: state.value === 'connected'
      property ContextProperty state

      onIsOnlineChanged: console.log('Env.Network.isOnline:', isOnline)

      state: ContextProperty {
          id: networkOnline
          key: 'Internet.NetworkState'
          onValueChanged: {
              console.log('Env.Network.state', value, isOnline)
          }
      }
  }

Both 'onIsOnlineChanged' triggers successfully, but not so much actually
using them:

harbour-myapp.qml:

  ApplicationWindow {
      property bool isOnline: Env.isOnline // Creates no binding

      // This isn't triggered
      onIsOnlineChanged: {
          console.log('App.onIsOnlineChanged', isOnline)
      }

      // Until I make this binding
      Binding { // This does create a binding
          target: app
          property: 'isOnline'
          value: Env.isOnline
      }
  }

Is there a way to have this binding without creating it explicitly?

Next question: I need the binding to work, because I don't want to fire
off any request before I know if the network is up. Is there anything
that can be done to shorten the relatively long time it takes for the
ContextProperty to register network connectivity?

-- 
/Thomas

A: Because it breaks the logical sequence of discussion
Q: Why is top posting bad?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 484 bytes
Desc: OpenPGP digital signature
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20190830/d89258e7/attachment.sig>


More information about the Devel mailing list