1

I'm trying to plot data in a QLabel widget using bar chart format. After the chart is created, I capture it using chartView->grab() for insertion into the QLabel. This approach works with a pie chart, but for a bar chart the child elements (QBarSeries and QBarSet) are not displayed.

The code is as follows:

QT_CHARTS_USE_NAMESPACE

QChartView* chartView = new QChartView();
chartView->setRenderHint(QPainter::Antialiasing);

QChart* chart = chartView->chart();

QBarSeries *series = new QBarSeries();

QBarCategoryAxis *categories = new QBarCategoryAxis();
QBarSet *set0 = new QBarSet("test");
set0->setBrush(Qt::blue);

for (int i = 0; i < 20; ++i) {
    set0->append(static_cast<double>(10-std::abs(10-i)));
    categories->append(QString::number(i));
}
series->append(set0);

chart->addSeries(series);
chart->setAnimationOptions(QChart::SeriesAnimations);
chart->setTitle("Plot Test");

chart->setAxisX(categories, series);
chart->createDefaultAxes();

chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);


*m_chart = chartView->grab();

This is the displayed image.

enter image description here

If instead, I do:

chartView->show();

A window pops up with QBarSeries data included. Any ideas on why the grab() method is not capturing the child elements?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

Answered on Qt forum: grab is capturing the first frame of an animation due to: QChart::SeriesAnimations setting. Removing it, fixes the problem.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241