PDA

View Full Version : Save QChartView as PNG



Carlsberg
24th June 2017, 06:08
I want to save a chart as PNG. I'm doing this:



QChartView* chartView = new QChartView;
chartView->setRenderHint(QPainter::Antialiasing);
chartView->resize(printer.width(), printer.width() * 0.60);

QChart* chart = new QChart;
chart->legend()->hide();
chartView->setChart(chart);

QDateTimeAxis* xAxis = new QDateTimeAxis;
xAxis->setFormat("dd.MM.yyyy");
chart->addAxis(xAxis, Qt::AlignBottom);

QValueAxis* yAxis = new QValueAxis;
chart->addAxis(yAxis, Qt::AlignLeft);

QLineSeries* series = new QLineSeries;
series->append(data);
series->setPointsVisible(true);

chart->addSeries(series);
series->attachAxis(xAxis);
series->attachAxis(yAxis);

QPixmap p = chartView->grab();
p.save("a.png", "PNG");

The problem is that the axes are not shown. If I show the widget the axes are visible, but even then they are not saved in the PNG...

Any idea how to make them appear in the image?

d_stranz
24th June 2017, 17:44
Try using QWidget::render() instead of QWidget::grab():



QPixmap p( chartView->size() );
chartView->render( &p );