Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Satellite Info#

The Satellite Info example shows the available satellites using Sky View, Table View, or RSSI View and the user’s current position. It is implemented with Qt Positioning and Qt Quick.

This example demonstrates the usage of Qt Positioning QML API :

The example also shows how to use a custom C++ model together with a custom proxy model from QML.

UI Overview#

The example shows satellite information in three different tabs. The data is taken from the SatelliteSource::satellitesInView and SatelliteSource::satellitesInUse properties.

../_images/skyview_tableview.webp

The Sky View tab shows the relative satellite positions using the Azimuth and Elevation attributes . Clicking on an individual satellite object opens a popup with the satellite identifier , its azimuth and elevation.

The Table View tab shows the list of all detected satellites, and allows to apply sorting and filtering to the list.

../_images/rssiview_settings.webp

The RSSI View tab shows the signal strength of satellites in view using the signalStrength property. The numbers below the bars represent the individual satellite identifiers .

The Sky View and RSSI View tabs also show the current latitude and longitude. They use PositionSource::position property to extract this information.

The Status block on top of the tabs shows the current mode or the last error.

The Settings menu allows to switch the application color mode and show the help information.

The application operates in three different modes:

Application mode

Description

Running

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

Stopped

The application stops updating the satellite and position information.

Single

The application makes a single satellite and position update request.

The application automatically switches into a simulation mode if the platform does not provide satellite or position information. The simulation mode uses an NMEA plugin with pre-recorded NMEA data.

Note

Apple does not provide any APIs to retrieve satellite information, so on macOS and iOS the satellite information will always be taken from pre-recorded data. These API limitations do not affect positioning information, so current position can be displayed correctly.

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.

Retrieving Current Position#

The current position is retrieved from the PositionSource QML object. The onPositionChanged handler is used to receive position updates. The string representations of latitude and longitude are extracted from the coordinate property.

PositionSource {
    id: positionSource
    name: root.simulation ? "nmea" : ""
onPositionChanged: {
    let posData = position.coordinate.toString().split(", ")
    positionBox.latitudeString = posData[0]
    positionBox.longitudeString = posData[1]
}
}

Retrieving Satellite Information#

Similarly to the position, the current satellite information is retrieved from the SatelliteSource QML object. The onSatellitesInViewChanged and onSatellitesInUseChanged handlers are used to get the updated satellites in view and satellites in use respectively. In this example, the data is then forwarded to the C++ model, which is later used in all views.

SatelliteSource {
    id: satelliteSource
    name: root.simulation ? "nmea" : ""
onSatellitesInViewChanged: root.satellitesModel.updateSatellitesInView(satellitesInView)
onSatellitesInUseChanged: root.satellitesModel.updateSatellitesInUse(satellitesInUse)
}

Note

The example shows both the QML Positioning API, and the integration of C++ model to QML. That is the reason why the satellite information is first retrieved in QML, then forwarded to C++, and then back to QML to be used in the model. In practice, if the application is supposed to use complex C++ models, consider directly using the QGeoSatelliteInfoSource class from C++.

Using Custom C++ Models#

The example makes use of two custom models - SatelliteModel and SortFilterModel.

Satellite Model#

The SatelliteModel class is derived from QAbstractListModel and reimplements the rowCount(), data(), and roleNames() methods to represent the satellite information. Using QAbstractListModel as a base class allows to easily use the model with the QML ListView and Repeater types. A custom size property is used only in the RSSI View tab to dynamically calculate the width of the tab bars.

class SatelliteModel(QAbstractListModel):

    Q_OBJECT
    Q_PROPERTY(int size READ rowCount NOTIFY sizeChanged)
    QML_ELEMENT
# public
    SatelliteModel = explicit(QObject parent = None)
    int rowCount(QModelIndex parent = QModelIndex()) override
    QVariant data(QModelIndex index, int role = Qt.DisplayRole) override
    QHash<int, QByteArray> roleNames() override
# public slots
    def updateSatellitesInView(inView):
    def updateSatellitesInUse(inUse):
# signals
    def sizeChanged():

The roleNames() method is used to map the model’s roles to the property names which can be used to access the model data from QML. For example, the id name is used to extract the satellite identifier, and the rssi name is used to get the signal strength.

QHash<int, QByteArray> SatelliteModel.roleNames()

    return {
        {Roles.IdRole, "id"},
        {Roles.RssiRole, "rssi"},
        {Roles.AzimuthRole, "azimuth"},
        {Roles.ElevationRole, "elevation"},
        {Roles.SystemRole, "system"},
        {Roles.SystemIdRole, "systemId"},
        {Roles.InUseRole, "inUse"},
        {Roles.VisibleNameRole, "name"}

On the QML side, we can use these names to get the actual values. For example, the implementation of RSSI View uses the rssi, inUse, and id role names to draw the bars that represent individual satellites:

Repeater {
    id: repeater
    model: root.satellitesModel
    delegate: Rectangle {
        required property var modelData
        height: rect.height
        width: view.singleWidth
        color: "transparent"
        SemiRoundedRectangle {
            anchors.bottom: satId.top
            width: parent.width
            height: (parent.height - satId.height)
                    * Math.min(parent.modelData.rssi, rect.maxVisibleLevel)
                    / rect.maxVisibleLevel
            color: parent.modelData.inUse ? root.inUseColor : root.inViewColor
        }
        Text {
            id: satId
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.bottom
            text: parent.modelData.id
            color: Theme.textSecondaryColor
            font.pixelSize: Theme.smallFontSize
            font.weight: Theme.fontLightWeight
        }
    }
}

Proxy Model#

The SortFilterModel class is used to provide custom sorting and filtering of the satellite objects shown in the Table View tab.

The model is derived from QSortFilterProxyModel and reimplements the filterAcceptsRow() and lessThan() methods to provide filtering and sorting. The model also exposes several slots to tune the filtering and sorting behavior.

class SortFilterModel(QSortFilterProxyModel):

    Q_OBJECT
    QML_ELEMENT
# public
    SortFilterModel = explicit(QObject parent = None)
# public slots
    def updateFilterString(str):
    def updateShowInView(show):
    def updateShowInUse(show):
    def updateSelectedSystems(id, show):
    def updateSortRoles(role, use):
# protected
    bool filterAcceptsRow(int row, QModelIndex parent) override
    bool lessThan(QModelIndex left, QModelIndex right) override

These slots can be called from both C++ and QML. For example, the Satellite Identifier delegate uses the updateSelectedSystems() slot to show or hide the information about satellites belonging to certain satellite systems. Likewise, the Satellite Status delegates use the updateShowInView() and updateShowInUse() slots to filter the satellites with a specific status.

Repeater {
    model: root.satelliteSystemModel
    delegate: CheckElement {
        required property var modelData
        text: modelData.name
        Layout.alignment: Qt.AlignRight
        onCheckedChanged: {
            root.sortFilterModel.updateSelectedSystems(modelData.id, checked)
        }
    }
}            ...

CheckElement {
    text: qsTr("In View")
    Layout.alignment: Qt.AlignRight
    onCheckedChanged: root.sortFilterModel.updateShowInView(checked)
}
CheckElement {
    text: qsTr("In Use")
    Layout.alignment: Qt.AlignRight
    onCheckedChanged: root.sortFilterModel.updateShowInUse(checked)
}

QML Module Registration#

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