Oscilloscopio QML
L'esempio illustra come implementare un'applicazione con requisiti prestazionali rigorosi utilizzando l'API QML Qt Charts.

L'applicazione oscilloscopio illustra come utilizzare l'API QML Qt Charts per implementare un'applicazione con requisiti di prestazioni rigorosi. L'applicazione utilizza dati generati con caratteristiche configurabili per simulare una semplice interfaccia utente di un oscilloscopio.
Per ottenere informazioni sulla velocità di rendering effettiva visualizzata nella console di output dell’applicazione, è possibile impostare QSG_RENDER_TIMING = 1 nelle impostazioni dell’ambiente di esecuzione. Per farlo, in Qt Creator, andare su Progetti - Esegui - Ambiente di esecuzione e selezionare Aggiungi. A questo punto è possibile sperimentare le diverse opzioni configurabili dell’applicazione di esempio per individuare la configurazione che garantisce le migliori prestazioni nel proprio ambiente.
Esecuzione dell’esempio
È possibile eseguire l'esempio da:
- Qt Creator
Aprire la modalità " Welcome " e selezionare l’esempio da Examples. Per ulteriori informazioni, consultare Qt Creator: Tutorial: Compilazione ed esecuzione.
- Qt Extension for Visual Studio Code
Eseguire il comando Qt: Open Qt examples da Command Palette e selezionare l’esempio dall’elenco. Per ulteriori informazioni, consultare Qt Extension for Visual Studio Code: Tutorial: Compilazione ed esecuzione.
Creazione delle viste
La finestra dell’applicazione è condivisa dalle viste “control” e “scope”:
Item {
id: main
width: 600
height: 400
ControlPanel {
id: controlPanel
anchors.top: parent.top
anchors.topMargin: 10
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.leftMargin: 10
...
ScopeView {
id: scopeView
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.left: controlPanel.right
height: main.height
}ControlView implementa i pulsanti utilizzati per la configurazione. ScopeView utilizza una ChartView per visualizzare un grafico con due serie di linee:
ChartView {
id: chartView
property bool openGL: openGLSupported
animationOptions: ChartView.NoAnimation
theme: ChartView.ChartThemeDark
onOpenGLChanged: {
if (openGLSupported) {
var series1 = series("signal 1")
if (series1)
series1.useOpenGL = openGL;
var series2 = series("signal 2")
if (series2)
series2.useOpenGL = openGL;
}
}
ValueAxis {
id: axisY1
min: -1
max: 4
}
ValueAxis {
id: axisY2
min: -10
max: 5
}
ValueAxis {
id: axisX
min: 0
max: 1024
}
LineSeries {
id: lineSeries1
name: "signal 1"
axisX: axisX
axisY: axisY1
useOpenGL: chartView.openGL
}
LineSeries {
id: lineSeries2
name: "signal 2"
axisX: axisX
axisYRight: axisY2
useOpenGL: chartView.openGL
}
...I dati delle serie di linee vengono aggiornati tramite un timer Qt Qml. In un'applicazione reale, l'aggiornamento potrebbe essere attivato da un segnale proveniente dal codice Qt C++.
Timer {
id: refreshTimer
interval: 1 / 60 * 1000 // 60 Hz
running: true
repeat: true
onTriggered: {
dataSource.update(chartView.series(0));
dataSource.update(chartView.series(1));
}
}L'oscilloscopio consente inoltre di cambiare il tipo di serie utilizzata per la visualizzazione delle sorgenti di segnale. Ciò viene implementato distruggendo e creando dinamicamente le serie:
function changeSeriesType(type) {
chartView.removeAllSeries();
// Create two new series of the correct type. Axis x is the same for both of the series,
// but the series have their own y-axes to make it possible to control the y-offset
// of the "signal sources".
var series1
var series2
if (type === "line") {
series1 = chartView.createSeries(ChartView.SeriesTypeLine, "signal 1",
axisX, axisY1);
series1.useOpenGL = chartView.openGL
series2 = chartView.createSeries(ChartView.SeriesTypeLine, "signal 2",
axisX, axisY2);
series2.useOpenGL = chartView.openGL
} else {
series1 = chartView.createSeries(ChartView.SeriesTypeScatter, "signal 1",
axisX, axisY1);
series1.markerSize = 2;
series1.borderColor = "transparent";
series1.useOpenGL = chartView.openGL
series2 = chartView.createSeries(ChartView.SeriesTypeScatter, "signal 2",
axisX, axisY2);
series2.markerSize = 2;
series2.borderColor = "transparent";
series2.useOpenGL = chartView.openGL
}
}
function createAxis(min, max) {
// The following creates a ValueAxis object that can be then set as a x or y axis for a series
return Qt.createQmlObject("import QtQuick 2.0; import QtCharts 2.0; ValueAxis { min: "
+ min + "; max: " + max + " }", chartView);
}© 2026 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.