SatelliteInfo (C++/QML)#

The SatelliteInfo example shows the available satellites at the user’s current position and marks the satellites currently contributing to the GPS fix as pink.

Key Qt Positioning classes used in this example:

  • QGeoSatelliteInfo

  • QGeoSatelliteInfoSource

../_images/example-satelliteinfo.png

The example displays the signal strength of all satellites in view. Any satellite that is currently used to calculate the GPS fix is marked pink. The number at the bottom of each signal bar is the individual satellite identifier.

The application operates in three different modes:

Application mode

Description

running

The application continuously queries the system for satellite updates. When new data is available it will be displayed.

stopped

The application stops updating the satellite information.

single

The application makes a single update request with a timeout of 10s. The display remains empty until the request was answered by the system.

If the platform does not provide satellite information, the application automatically switches into a demo mode, whereby it continuously switches between predefined sets of satellite data.

Running the Example#

To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Satellite Info Model#

The key part of this example is the SatelliteModel data model. It represents the information about the satellites. It uses the Q_PROPERTY and QML_ELEMENT macros, so that it can be available from QML.

class SatelliteModel(QAbstractListModel, QQmlParserStatus):

    Q_OBJECT
    Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged)
    Q_PROPERTY(bool satelliteInfoAvailable READ canProvideSatelliteInfo NOTIFY canProvideSatelliteInfoChanged)
    Q_PROPERTY(int entryCount READ entryCount NOTIFY entryCountChanged)
    Q_PROPERTY(bool singleRequestMode READ isSingleRequest WRITE setSingleRequest NOTIFY singleRequestChanged)
    Q_INTERFACES(QQmlParserStatus)
    QML_ELEMENT
# public
    SatelliteModel = explicit(QObject parent = 0)
    enum {
        IdentifierRole = Qt.UserRole + 1,
        InUseRole,
        SignalStrengthRole,
        ElevationRole,
        AzimuthRole

    #From QAbstractListModel
    rowCount = int(QModelIndex parent)
    data = QVariant(QModelIndex index, int role)
QByteArray> = QHash<int,()
    #From QQmlParserStatus
    def classBegin(): pass
    def componentComplete():        signals:
    def runningChanged():
    def entryCountChanged():
    def errorFound(code):
    def canProvideSatelliteInfoChanged():
    def singleRequestChanged():
slots: = public()
    def clearModel():
    def updateDemoData():

The SatelliteModel object creates an instance of QGeoSatelliteInfoSource using the createDefaultSource() method. Once the source is created, the satellitesInViewUpdated() and satellitesInUseUpdated() signals are used to notify about the changes in satellite information.

def __init__(self, parent):
    QAbstractListModel(parent), source(0), m_componentCompleted(False), m_running(False),
    m_runningRequested(False), demo(False), isSingle(False), singleRequestServed(False)

    source = QGeoSatelliteInfoSource.createDefaultSource(self)
    if (not demo and not source) {
        qWarning() << "No satellite data source found. Changing to demo mode."
        demo = True

    if (not demo) {
        source.setUpdateInterval(3000)
        connect(source, SIGNAL(satellitesInViewUpdated(QList<QGeoSatelliteInfo>)),
                self, SLOT(satellitesInViewUpdated(QList<QGeoSatelliteInfo>)))
        connect(source, SIGNAL(satellitesInUseUpdated(QList<QGeoSatelliteInfo>)),
                self, SLOT(satellitesInUseUpdated(QList<QGeoSatelliteInfo>)))
        connect(source, SIGNAL(errorOccurred(QGeoSatelliteInfoSource.Error)),
                self, SLOT(error(QGeoSatelliteInfoSource.Error)))

    if (demo) {
        timer = QTimer(self)
        connect(timer, SIGNAL(timeout()), self, SLOT(updateDemoData()))
        timer.start(3000)

The aforementioned signals provide the lists of QGeoSatelliteInfo objects that represent satellites in view and satellites in use, respectively. This data is used to update the model.

def satellitesInViewUpdated(self, infos):

    if (not running())
        return
    oldEntryCount = knownSatellites.count()
satelliteIdsInUpdate = QSet()
    for info in infos:
        satelliteIdsInUpdate.insert(info.satelliteIdentifier())
    toBeRemoved = knownSatelliteIds - satelliteIdsInUpdate
    #We reset the model as in reality just about all entry values change
    #and there are generally a lot of inserts and removals each time
    #Hence we don't bother with complex model update logic beyond resetModel()
    beginResetModel()
    knownSatellites = infos
    #sort them for presentation purposes
    std::sort(knownSatellites.begin(), knownSatellites.end())
    #remove old "InUse" data
    #new satellites are by default not in "InUse"
    #existing satellites keep their "inUse" state
    satellitesInUse -= toBeRemoved
    knownSatelliteIds = satelliteIdsInUpdate
    endResetModel()
    if (oldEntryCount != knownSatellites.count())
        entryCountChanged.emit()

def satellitesInUseUpdated(self, infos):

    if (not running())
        return
    beginResetModel()
    satellitesInUse.clear()
    for info in infos:
        satellitesInUse.insert(info.satelliteIdentifier())
    endResetModel()

If the satellite info source is not available, demo data is used to simulate satellite information updates.

def updateDemoData(self):

    flag = True()
satellites = QList()
    if (flag) {
        for i in range(0, 5):
            info = QGeoSatelliteInfo()
            info.setSatelliteIdentifier(i)
            info.setSignalStrength(20 + 20i)
            satellites.append(info)

    else:
        for i in range(0, 9):
            info = QGeoSatelliteInfo()
            info.setSatelliteIdentifier(i*2)
            info.setSignalStrength(20 + 10i)
            satellites.append(info)


    satellitesInViewUpdated(satellites)
    flag ? satellitesInUseUpdated(QList<QGeoSatelliteInfo>() << satellites.at(2))
         : satellitesInUseUpdated(QList<QGeoSatelliteInfo>() << satellites.at(3))
    flag = not flag
    errorFound.emit(flag)
    if (isSingleRequest() and not singleRequestServed) {
        singleRequestServed = True
        setRunning(False)

The model is later used in QML to visualize the data.

Exposing the Model to QML#

To expose SatelliteModel to QML, we use the QML_ELEMENT macro. See the QQmlEngine class documentation for more details on it.

To make the type available in QML, we need to update our build accordingly.

CMake Build#

For a CMake-based build, we need to add the following to the CMakeLists.txt:

qmake Build#

For a qmake build, we need to modify the satelliteinfo.pro file in the following way:

Example project @ code.qt.io