Chart Themes Example

The example shows the look and feel of the different built-in themes.

This example shows the look and feel of the different built-in themes for some of the supported chart types.

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.

Creating Charts

The charts of different types are generated and added to the layout separately. For example, the line chart is created as follows. The creation of other chart types is similar.

First a chart is created.

QChart *chart = new QChart();
chart->setTitle("Line chart");

A common set of random data is generated and placed in a list. This list is used in each chart type to add data to the series of the chart. For the line series, QLineSeries instances are created and added to the chart.

QString name("Series ");
int nameIndex = 0;
for (const DataList &list : m_dataTable) {
    QLineSeries *series = new QLineSeries(chart);
    for (const Data &data : list)
    series->setName(name + QString::number(nameIndex));

Default axes are created for the line series. We also specify ranges for the axes based on the range of the data used for the series.

chart->axisX()->setRange(0, m_valueMax);
chart->axisY()->setRange(0, m_valueCount);

We also want to add more space between the labels and the y-axes. For this we specify a label format that adds space characters to the labels.

// Add space to label to add space between labels and axis
static_cast<QValueAxis *>(chart->axisY())->setLabelFormat("%.1f  ");

Finally the line chart is added to the grid layout.

chartView = new QChartView(createLineChart());
m_ui->gridLayout->addWidget(chartView, 1, 2);

Changing Theme

The user can select a built-in theme to be used in the example. This theme is then applied to all charts in the layout.

QChart::ChartTheme theme = static_cast<QChart::ChartTheme>(

In order to give the result a more harmonious look, the background palette of the application is customized to match the selected theme. The QPalette::Window and QPalette::WindowText roles are set based on the selected theme.

QPalette pal = window()->palette();
if (theme == QChart::ChartThemeLight) {
    pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
    pal.setColor(QPalette::WindowText, QRgb(0x404044));

Changing Animation, Legend and Anti-Aliasing

In this example, it is also possible to see how changing animation, legend and anti-aliasing affects the appearance of the chart.

Based on the user's selection, the used animation type is set on each chart. It is possible to have no animations in the chart, or have animations for grid axis or series, or both.

QChart::AnimationOptions options(
if (!m_charts.isEmpty() &&>chart()->animationOptions() != options) {
    for (QChartView *chartView : charts)

The chart can be shown with a legend. The legend can be aligned to different sides of the chart.

Qt::Alignment alignment(

if (!alignment) {
    for (QChartView *chartView : charts)
} else {
    for (QChartView *chartView : charts) {

The user can also see how changing anti-aliasing option changes the appearance of the chart. Anti-aliasing is updated based on the user's selection.

bool checked = m_ui->antialiasCheckBox->isChecked();
for (QChartView *chart : charts)
    chart->setRenderHint(QPainter::Antialiasing, checked);


Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
create apps
create games
cross platform
native performance
3rd party services
game network
level editor
easiest to learn
biggest time saving
best support