MonitorModel QML Type

A model that can fetch data from various sources and keep a history of their values. More...

Import Statement: import QtApplicationManager 2.0

Properties

Methods

Detailed Description

MonitorModel can fetch data from various sources at regular intervals and keep a history of their values. Its main use is having it as a model to plot historical data in a graph for monitoring purposes, such as a CPU usage graph.

The snippet below shows how to use it for plotting a system's CPU load in a simple bar graph:

import QtQuick 2.11
import QtApplicationManager 2.0

ListView {
    id: listView
    width: 400
    height: 100
    orientation: ListView.Horizontal
    spacing: (width / model.count) * 0.2
    clip: true
    interactive: false

    model: MonitorModel {
        id: monitorModel
        running: listView.visible
        CpuStatus {}
    }

    delegate: Rectangle {
        width: (listView.width / monitorModel.count) * 0.8
        height: model.cpuLoad * listView.height
        y: listView.height - height
        color: "blue"
    }
}

To add a data source to MonitorModel just declare it inside the model, as done in the example above with the CpuStatus component. Alternatively (such as from imperative javscript code) you can add data sources by assigning them to MonitorModel's dataSources property.

A data source can be any QtObject with the following characteristics:

  • A roleNames property: it's a list of strings naming the roles that this data source provides. Those role names will be available on each row created by MonitorModel.
  • Properties matching the names provided in the roleNames property: MonitorModel will query their values when building each new model row.
  • An update() function: MonitorModel will call it before creating each new model row, so that the data source can update the values of its properties.

The following snippet shows MonitorModel using a custom data source written in QML:

MonitorModel {
    running: true
    QtObject {
        property var roleNames: ["foo", "bar"]

        function update() {
            // foo will have ever increasing values
            foo += 1;

            // bar will keep oscillating between 0 and 10
            if (up) {
                bar += 1;
                if (bar == 10)
                    up = false;
            } else {
                bar -= 1;
                if (bar == 0)
                    up = true;
            }
        }

        property int foo: 0
        property int bar: 10
        property bool up: false
    }
}

Thus, in the MonitorModel above, every row will have two roles: foo and bar. If plotted, you would see an ever incresing foo and an oscillating bar.

QtApplicationManager comes with a number of components that are readily usable as data sources, namely:

While running is true, MonitorModel will probe its data sources every interval milliseconds, creating a new row every time up to maximumCount. Once that value is reached the oldest row (the first one) is discarded whenever a new row comes in, so that count doesn't exceed maximumCount. New rows are always appended to the model, so rows are ordered chronologically from oldest (index 0) to newest (index count-1).

Property Documentation

count : int [read-only]

Number of rows in the model. It ranges from zero up to MonitorModel::maximumCount.

See also MonitorModel::maximumCount and MonitorModel::clear.


dataSources : list<object> [default]

List of data sources for the MonitorModel to use. A data source can be any QtObject containing at least a roleNames property and an update() function. For more information, see detailed description above.


interval : int

Interval, in milliseconds, between model updates (while MonitorModel::running). The default value is 1000.

See also MonitorModel::running.


maximumCount : int

The maximum number of rows that the MonitorModel will keep. After this limit is reached the oldest rows start to get discarded to make room for the new ones coming in.

See also MonitorModel::count and MonitorModel::clear.


running : bool

While true, MonitorModel will keep probing its data sources and adding new rows every MonitorModel::interval milliseconds. The default value is false.

Normally you have this property set to true only while the data is being displayed.

See also MonitorModel::interval.


Method Documentation

clear()

Empties the model, removing all exising rows.

See also MonitorModel::count.


object get(int index)

Returns the model data for the reading point identified by index as a JavaScript object. The index must be in the range [0, count); returns an empty object otherwise.


© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.