SelectedBar Example▲
The example shows changing the color and state of bars using the selection feature.
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.
Using setBarSelected()▲
We create the sets and fill them with the data. Then we create a series and append data to it.
QBarSet *
setChicken =
Utilities::
createChickenSet();
QBarSet *
setPork =
Utilities::
createPorkSet();
QBarSet *
setTurkey =
Utilities::
createTurkeySet();
QBarSet *
setHam =
Utilities::
createHamSet();
qreal totalSum =
setChicken-&
gt;sum() +
setPork-&
gt;sum() +
setTurkey-&
gt;sum() +
setHam-&
gt;sum();
QList&
lt;QBarSet *&
gt; setList =
QList&
lt;QBarSet *&
gt;{
setChicken, setPork, setTurkey, setHam}
;
QBarSeries *
series =
new
QBarSeries();
series-&
gt;append(setList);
We create the chart and add series to it. Also, we add a title to the chart, set animation for the chart, and align the legend.
QChart *
chart =
new
QChart();
chart-&
gt;addSeries(series);
chart-&
gt;setTitle(QCoreApplication::
tr("Meat consumption"
));
chart-&
gt;setAnimationOptions(QChart::
SeriesAnimations);
chart-&
gt;legend()-&
gt;setVisible(true
);
chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignBottom);
Here we set the color for the selected bars.
for
(QBarSet *
barSet : series-&
gt;barSets())
barSet-&
gt;setSelectedColor(barSet-&
gt;brush().color().darker());
Next step is adding axes: QBarCategoryAxis for years of measurements and QValueAxis for values range.
QStringList categories =
Utilities::
createYearCategories();
QBarCategoryAxis *
axisX =
new
QBarCategoryAxis();
axisX-&
gt;setCategories(categories);
chart-&
gt;addAxis(axisX, Qt::
AlignBottom);
series-&
gt;attachAxis(axisX);
QValueAxis *
axisY =
new
QValueAxis();
axisY-&
gt;setRange(0
, 20
);
axisY-&
gt;setTitleText(QCoreApplication::
tr("Tons"
));
axisY-&
gt;setLabelsAngle(-
90
);
axisY-&
gt;setTitleVisible(true
);
chart-&
gt;addAxis(axisY, Qt::
AlignLeft);
series-&
gt;attachAxis(axisY);
Then we add the chart view to put the chart in.
QChartView *
chartView =
new
QChartView(chart);
chartView-&
gt;setRenderHint(QPainter::
Antialiasing);
Here we create a widget for labels of values of selected and unselected bars.
QWidget *
labelWidget =
new
QWidget(&
amp;mainWindow);
QHBoxLayout *
labelLayout =
new
QHBoxLayout(labelWidget);
labelLayout-&
gt;setAlignment(Qt::
AlignCenter);
QLabel *
totalSumLabel =
new
QLabel(QCoreApplication::
tr("Total sum: %1 T"
).arg(totalSum));
labelLayout-&
gt;addWidget(totalSumLabel);
totalSumLabel-&
gt;setContentsMargins(0
, 0
, 54
, 0
);
QLabel *
selectedSumLabel =
new
QLabel(QCoreApplication::
tr("Selected sum: 0 T"
));
labelLayout-&
gt;addWidget(selectedSumLabel);
QLabel *
unselectedSumLabel =
new
QLabel(QCoreApplication::
tr("Unselected sum: %1 T"
).arg(totalSum));
labelLayout-&
gt;addWidget(unselectedSumLabel);
unselectedSumLabel-&
gt;setContentsMargins(54
, 0
, 0
, 0
);
We connect selecting of a specific bar with labels of values using a lambda. set->toggleSelection({index}) sets the bar selected.
QObject::
connect(series, &
amp;QAbstractBarSeries::
clicked, series, [=
](int
index, QBarSet *
set) {
set-&
gt;toggleSelection({
index}
);
qreal selectedSum =
0.
;
for
(int
i =
0
; i &
lt; setList.size(); ++
i) {
auto
selectedIndices =
setList.at(i)-&
gt;selectedBars();
for
(int
k =
0
; k &
lt; selectedIndices.size(); ++
k)
selectedSum +=
setList.at(i)-&
gt;at(selectedIndices.at(k));
}
selectedSumLabel-&
gt;setText(QCoreApplication::
tr("Selected sum: %1 T"
).arg(selectedSum));
// Because of rounding errors, selectedSum can result in being bigger than total sum
qreal unselectedSum =
totalSum -
selectedSum &
lt; 0
? 0.
: totalSum -
selectedSum;
unselectedSumLabel-&
gt;setText(
QCoreApplication::
tr("Unselected sum: %1 T"
)
.arg(unselectedSum)
);
}
);
Finally, we create the main widget and add other layouts to it and run the application.
QWidget *
mainWidget =
new
QWidget(&
amp;mainWindow);
QVBoxLayout *
mainLayout =
new
QVBoxLayout(mainWidget);
mainLayout-&
gt;addWidget(chartView);
mainLayout-&
gt;addWidget(labelWidget);
mainWindow.setCentralWidget(mainWidget);
mainWindow.resize(800
, 600
);
mainWindow.show();
return
a.exec();