Legend 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
"mainwidget.h"
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QtCore/QDebug>
#include <QtCharts/QBarSet>
#include <QtCharts/QBarSeries>
#include <QtCharts/QLegend>
#include <QtWidgets/QFormLayout>
QT_CHARTS_USE_NAMESPACE
MainWidget::
MainWidget(QWidget *
parent) :
QWidget(parent)
{
// Create buttons for ui
m_buttonLayout =
new
QGridLayout();
QPushButton *
detachLegendButton =
new
QPushButton("Toggle attached"
);
connect(detachLegendButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
toggleAttached);
m_buttonLayout-&
gt;addWidget(detachLegendButton, 0
, 0
);
QPushButton *
addSetButton =
new
QPushButton("add barset"
);
connect(addSetButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
addBarset);
m_buttonLayout-&
gt;addWidget(addSetButton, 2
, 0
);
QPushButton *
removeBarsetButton =
new
QPushButton("remove barset"
);
connect(removeBarsetButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
removeBarset);
m_buttonLayout-&
gt;addWidget(removeBarsetButton, 3
, 0
);
QPushButton *
alignButton =
new
QPushButton("Align (Bottom)"
);
connect(alignButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
setLegendAlignment);
m_buttonLayout-&
gt;addWidget(alignButton, 4
, 0
);
QPushButton *
boldButton =
new
QPushButton("Toggle bold"
);
connect(boldButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
toggleBold);
m_buttonLayout-&
gt;addWidget(boldButton, 8
, 0
);
QPushButton *
italicButton =
new
QPushButton("Toggle italic"
);
connect(italicButton, &
amp;QPushButton::
clicked, this
, &
amp;MainWidget::
toggleItalic);
m_buttonLayout-&
gt;addWidget(italicButton, 9
, 0
);
m_legendPosX =
new
QDoubleSpinBox();
m_legendPosY =
new
QDoubleSpinBox();
m_legendWidth =
new
QDoubleSpinBox();
m_legendHeight =
new
QDoubleSpinBox();
connect(m_legendPosX,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateLegendLayout);
connect(m_legendPosY,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateLegendLayout);
connect(m_legendWidth,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateLegendLayout);
connect(m_legendHeight,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
updateLegendLayout);
QFormLayout *
legendLayout =
new
QFormLayout();
legendLayout-&
gt;addRow("HPos"
, m_legendPosX);
legendLayout-&
gt;addRow("VPos"
, m_legendPosY);
legendLayout-&
gt;addRow("Width"
, m_legendWidth);
legendLayout-&
gt;addRow("Height"
, m_legendHeight);
m_legendSettings =
new
QGroupBox("Detached legend"
);
m_legendSettings-&
gt;setLayout(legendLayout);
m_buttonLayout-&
gt;addWidget(m_legendSettings);
m_legendSettings-&
gt;setVisible(false
);
// Create chart view with the chart
m_chart =
new
QChart();
m_chartView =
new
QChartView(m_chart, this
);
// Create spinbox to modify font size
m_fontSize =
new
QDoubleSpinBox();
m_fontSize-&
gt;setValue(m_chart-&
gt;legend()-&
gt;font().pointSizeF());
connect(m_fontSize,
static_cast
&
lt;void
(QDoubleSpinBox::
*
)(double
)&
gt;(&
amp;QDoubleSpinBox::
valueChanged),
this
, &
amp;MainWidget::
fontSizeChanged);
QFormLayout *
fontLayout =
new
QFormLayout();
fontLayout-&
gt;addRow("Legend font size"
, m_fontSize);
// Create layout for grid and detached legend
m_mainLayout =
new
QGridLayout();
m_mainLayout-&
gt;addLayout(m_buttonLayout, 0
, 0
);
m_mainLayout-&
gt;addLayout(fontLayout, 1
, 0
);
m_mainLayout-&
gt;addWidget(m_chartView, 0
, 1
, 3
, 1
);
setLayout(m_mainLayout);
createSeries();
}
void
MainWidget::
createSeries()
{
m_series =
new
QBarSeries();
addBarset();
addBarset();
addBarset();
addBarset();
m_chart-&
gt;addSeries(m_series);
m_chart-&
gt;setTitle("Legend detach example"
);
m_chart-&
gt;createDefaultAxes();
m_chart-&
gt;legend()-&
gt;setVisible(true
);
m_chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignBottom);
m_chartView-&
gt;setRenderHint(QPainter::
Antialiasing);
}
void
MainWidget::
showLegendSpinbox()
{
m_legendSettings-&
gt;setVisible(true
);
QRectF chartViewRect =
m_chartView-&
gt;rect();
m_legendPosX-&
gt;setMinimum(0
);
m_legendPosX-&
gt;setMaximum(chartViewRect.width());
m_legendPosX-&
gt;setValue(150
);
m_legendPosY-&
gt;setMinimum(0
);
m_legendPosY-&
gt;setMaximum(chartViewRect.height());
m_legendPosY-&
gt;setValue(150
);
m_legendWidth-&
gt;setMinimum(0
);
m_legendWidth-&
gt;setMaximum(chartViewRect.width());
m_legendWidth-&
gt;setValue(150
);
m_legendHeight-&
gt;setMinimum(0
);
m_legendHeight-&
gt;setMaximum(chartViewRect.height());
m_legendHeight-&
gt;setValue(75
);
}
void
MainWidget::
hideLegendSpinbox()
{
m_legendSettings-&
gt;setVisible(false
);
}
void
MainWidget::
toggleAttached()
{
QLegend *
legend =
m_chart-&
gt;legend();
if
(legend-&
gt;isAttachedToChart()) {
legend-&
gt;detachFromChart();
m_chart-&
gt;legend()-&
gt;setBackgroundVisible(true
);
m_chart-&
gt;legend()-&
gt;setBrush(QBrush(QColor(128
, 128
, 128
, 128
)));
m_chart-&
gt;legend()-&
gt;setPen(QPen(QColor(192
, 192
, 192
, 192
)));
showLegendSpinbox();
updateLegendLayout();
}
else
{
legend-&
gt;attachToChart();
legend-&
gt;setBackgroundVisible(false
);
hideLegendSpinbox();
}
update();
}
void
MainWidget::
addBarset()
{
QBarSet *
barSet =
new
QBarSet(QString("set "
) +
QString::
number(m_series-&
gt;count()));
qreal delta =
m_series-&
gt;count() *
0.1
;
*
barSet &
lt;&
lt; 1
+
delta &
lt;&
lt; 2
+
delta &
lt;&
lt; 3
+
delta &
lt;&
lt; 4
+
delta;
m_series-&
gt;append(barSet);
}
void
MainWidget::
removeBarset()
{
QList&
lt;QBarSet *&
gt; sets =
m_series-&
gt;barSets();
if
(sets.count() &
gt; 0
) {
m_series-&
gt;remove(sets.at(sets.count() -
1
));
}
}
void
MainWidget::
setLegendAlignment()
{
QPushButton *
button =
qobject_cast&
lt;QPushButton *&
gt;(sender());
switch
(m_chart-&
gt;legend()-&
gt;alignment()) {
case
Qt::
AlignTop:
m_chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignLeft);
if
(button)
button-&
gt;setText("Align (Left)"
);
break
;
case
Qt::
AlignLeft:
m_chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignBottom);
if
(button)
button-&
gt;setText("Align (Bottom)"
);
break
;
case
Qt::
AlignBottom:
m_chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignRight);
if
(button)
button-&
gt;setText("Align (Right)"
);
break
;
default
:
if
(button)
button-&
gt;setText("Align (Top)"
);
m_chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignTop);
break
;
}
}
void
MainWidget::
toggleBold()
{
QFont font =
m_chart-&
gt;legend()-&
gt;font();
font.setBold(!
font.bold());
m_chart-&
gt;legend()-&
gt;setFont(font);
}
void
MainWidget::
toggleItalic()
{
QFont font =
m_chart-&
gt;legend()-&
gt;font();
font.setItalic(!
font.italic());
m_chart-&
gt;legend()-&
gt;setFont(font);
}
void
MainWidget::
fontSizeChanged()
{
QFont font =
m_chart-&
gt;legend()-&
gt;font();
font.setPointSizeF(m_fontSize-&
gt;value());
m_chart-&
gt;legend()-&
gt;setFont(font);
}
void
MainWidget::
updateLegendLayout()
{
m_chart-&
gt;legend()-&
gt;setGeometry(QRectF(m_legendPosX-&
gt;value(),
m_legendPosY-&
gt;value(),
m_legendWidth-&
gt;value(),
m_legendHeight-&
gt;value()));
m_chart-&
gt;legend()-&
gt;update();
}