i am trying to create an application which can plot large data sets (so using of OpenGl is important to me). I use QChartView
, QChart
and QLineSeries
. Also for QLineSeries
i turn using openGL on. But when i'm trying to save chart as an image i get plot without data. I know that when QLineSeries
using openGL it creates a QOpenGLWidget
on top of the chart plot area, but i don't know ho to access it.
So a question is: how to save a chart as an image with drawn lines?
Some images:
What i want (Plot without using openGL):
What i get (Plot with openGL):
Here is an example of code:
MainWindow constructor:
chartView = new QChartView(generate_sin_chart(), ui->centralWidget);
ui->centralWidget->layout()->addWidget(chartView);
generate_sin_chart():
QLineSeries *series = new QLineSeries();
series->setUseOpenGL(true); //this line cause a problem
constexpr double PI = 3.14159265359;
for(int i = 0; i < 100; ++i)
{
double temp = i*PI/6;
series->append(temp, sin(temp));
}
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
return chart;
Function for saving:
QString filename = QFileDialog::getSaveFileName(this, tr("Save file"), "", tr("Images (*.png)"));
QPixmap p = chartView->grab();
p.save(filename, "PNG");