Solved Qt5 - chartView->grab() doesn't capture child elements
-
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:
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? -
chart->setAnimationOptions(QChart::SeriesAnimations);
that makes the series grow in an animation. you are probably capturing the first frame of that animation. -
That was it. Removing the call to:
chart->setAnimationOptions(QChart::SeriesAnimations);
resolved the issue.
Thanks!