OpenGL Accelerated Series Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"datasource.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QLogValueAxis>
#include <QtWidgets/QLabel>
// Uncomment to use logarithmic axes instead of regular value axes
//#define USE_LOG_AXIS
// Uncomment to use regular series instead of OpenGL accelerated series
//#define DONT_USE_GL_SERIES
// Uncomment to add a simple regular series (thick line) and a matching OpenGL series (thinner line)
// to verify the series have same visible geometry.
//#define ADD_SIMPLE_SERIES
QT_CHARTS_USE_NAMESPACE
int
main(int
argc, char
*
argv[])
{
QApplication a(argc, argv);
QStringList colors;
colors &
lt;&
lt; "red"
&
lt;&
lt; "blue"
&
lt;&
lt; "green"
&
lt;&
lt; "black"
;
QChart *
chart =
new
QChart();
chart-&
gt;legend()-&
gt;hide();
#ifdef USE_LOG_AXIS
QLogValueAxis *
axisX =
new
QLogValueAxis;
QLogValueAxis *
axisY =
new
QLogValueAxis;
#else
QValueAxis *
axisX =
new
QValueAxis;
QValueAxis *
axisY =
new
QValueAxis;
#endif
chart-&
gt;addAxis(axisX, Qt::
AlignBottom);
chart-&
gt;addAxis(axisY, Qt::
AlignLeft);
const
int
seriesCount =
10
;
#ifdef DONT_USE_GL_SERIES
const
int
pointCount =
100
;
chart-&
gt;setTitle("Unaccelerated Series"
);
#else
const
int
pointCount =
10000
;
chart-&
gt;setTitle("OpenGL Accelerated Series"
);
#endif
QList&
lt;QXYSeries *&
gt; seriesList;
for
(int
i =
0
; i &
lt; seriesCount; i++
) {
QXYSeries *
series =
0
;
int
colorIndex =
i %
colors.size();
if
(i %
2
) {
series =
new
QScatterSeries;
QScatterSeries *
scatter =
static_cast
&
lt;QScatterSeries *&
gt;(series);
scatter-&
gt;setColor(QColor(colors.at(colorIndex)));
scatter-&
gt;setMarkerSize(qreal(colorIndex +
2
) /
2.0
);
// Scatter pen doesn't have affect in OpenGL drawing, but if you disable OpenGL drawing
// this makes the marker border visible and gives comparable marker size to OpenGL
// scatter points.
scatter-&
gt;setPen(QPen("black"
));
}
else
{
series =
new
QLineSeries;
series-&
gt;setPen(QPen(QBrush(QColor(colors.at(colorIndex))),
qreal(colorIndex +
2
) /
2.0
));
}
seriesList.append(series);
#ifdef DONT_USE_GL_SERIES
series-&
gt;setUseOpenGL(false
);
#else
series-&
gt;setUseOpenGL(true
);
#endif
chart-&
gt;addSeries(series);
series-&
gt;attachAxis(axisX);
series-&
gt;attachAxis(axisY);
}
if
(axisX-&
gt;type() ==
QAbstractAxis::
AxisTypeLogValue)
axisX-&
gt;setRange(0.1
, 20.0
);
else
axisX-&
gt;setRange(0
, 20.0
);
if
(axisY-&
gt;type() ==
QAbstractAxis::
AxisTypeLogValue)
axisY-&
gt;setRange(0.1
, 10.0
);
else
axisY-&
gt;setRange(0
, 10.0
);
#ifdef ADD_SIMPLE_SERIES
QLineSeries *
simpleRasterSeries =
new
QLineSeries;
*
simpleRasterSeries &
lt;&
lt; QPointF(0.001
, 0.001
)
&
lt;&
lt; QPointF(2.5
, 8.0
)
&
lt;&
lt; QPointF(5.0
, 4.0
)
&
lt;&
lt; QPointF(7.5
, 9.0
)
&
lt;&
lt; QPointF(10.0
, 0.001
)
&
lt;&
lt; QPointF(12.5
, 2.0
)
&
lt;&
lt; QPointF(15.0
, 1.0
)
&
lt;&
lt; QPointF(17.5
, 6.0
)
&
lt;&
lt; QPointF(20.0
, 10.0
);
simpleRasterSeries-&
gt;setUseOpenGL(false
);
simpleRasterSeries-&
gt;setPen(QPen(QBrush("magenta"
), 8
));
chart-&
gt;addSeries(simpleRasterSeries);
simpleRasterSeries-&
gt;attachAxis(axisX);
simpleRasterSeries-&
gt;attachAxis(axisY);
QLineSeries *
simpleGLSeries =
new
QLineSeries;
simpleGLSeries-&
gt;setUseOpenGL(true
);
simpleGLSeries-&
gt;setPen(QPen(QBrush("black"
), 2
));
simpleGLSeries-&
gt;replace(simpleRasterSeries-&
gt;points());
chart-&
gt;addSeries(simpleGLSeries);
simpleGLSeries-&
gt;attachAxis(axisX);
simpleGLSeries-&
gt;attachAxis(axisY);
#endif
QChartView *
chartView =
new
QChartView(chart);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(600
, 400
);
window.show();
DataSource dataSource;
dataSource.generateData(seriesCount, 10
, pointCount);
QLabel *
fpsLabel =
new
QLabel(&
amp;window);
QLabel *
countLabel =
new
QLabel(&
amp;window);
QString countText =
QStringLiteral("Total point count: %1"
);
countLabel-&
gt;setText(countText.arg(pointCount *
seriesCount));
countLabel-&
gt;adjustSize();
fpsLabel-&
gt;move(10
, 2
);
fpsLabel-&
gt;adjustSize();
fpsLabel-&
gt;raise();
fpsLabel-&
gt;show();
countLabel-&
gt;move(10
, fpsLabel-&
gt;height());
fpsLabel-&
gt;raise();
countLabel-&
gt;show();
// We can get more than one changed event per frame, so do async update.
// This also allows the application to be responsive.
QObject::
connect(chart-&
gt;scene(), &
amp;QGraphicsScene::
changed,
&
amp;dataSource, &
amp;DataSource::
handleSceneChanged);
dataSource.startUpdates(seriesList, fpsLabel);
return
a.exec();
}