我正在尝试创建一个可以绘制大型数据集的应用程序(因此,使用OpenGl对我很重要)。我用的QChartView
,QChart
和QLineSeries
。也为QLineSeries
我打开使用openGL。但是,当我尝试将图表另存为图像时,会得到没有数据的图表。我知道QLineSeries
使用openGL时会QOpenGLWidget
在图表绘图区域的顶部创建一个,但我不知道该如何访问它。
因此,问题是:如何将图表另存为带有绘制线条的图像?
一些图像:
我想要什么(不使用openGL绘制):
我得到了什么(用openGL绘制):
这是代码示例:
MainWindow构造函数:
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");
According to the docs:
useOpenGL : bool
Specifies whether or not drawing the series is accelerated by using OpenGL.
Acceleration using OpenGL is supported only for QLineSeries and QScatterSeries. A line series used as an edge series for QAreaSeries cannot use OpenGL acceleration. When a chart contains any series that are drawn with OpenGL, a transparent QOpenGLWidget is created on top of the chart plot area. The accelerated series are not drawn on the underlying QGraphicsView, but are instead drawn on the created QOpenGLWidget.
[...]
So when you use grab()
only take a picture of the QChartView
, the solution is to find the QOpenGLWidget
object and record its image on top of the QChartView
image, the following code does it:
QPixmap p = chartView->grab();
QOpenGLWidget *glWidget = chartView->findChild<QOpenGLWidget*>();
if(glWidget){
QPainter painter(&p);
QPoint d = glWidget->mapToGlobal(QPoint())-chartView.mapToGlobal(QPoint());
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
painter.drawImage(d, glWidget->grabFramebuffer());
painter.end();
}
p.save("test", "PNG");
在使用时QOpenGLWidget
,必须添加QT += opengl
到.pro中
完整的示例可以在以下链接中找到
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句