我试图在本例中的QTextDocument中将图表保存到文件中:
QTextDocument doc("Frame rate test\n");
QTextCursor cursor(&doc);
cursor.movePosition(QTextCursor::End);
if (getTestFinishedStatus())
{
QPixmap pix = _pFrameRateChart->grab(); //_pFrameRateChart is QChartView
cursor.insertImage(pix.toImage());
}
QTextDocumentWriter docWriter;
docWriter.setFileName("framerate.odf");
docWriter.setFormat("ODF");
docWriter.write(&doc);
问题是,如果我在ui中显示图表,结果就不一样了。以下是未显示的结果:
以下是显示结果:
显然,我希望获得第二个结果,即使我没有将ChartView添加到小部件以在ui上显示它。我试过调整QChartView大小,调整QChart大小,将图表添加到临时小部件中,然后QVBoxLayout保存它,在保存它之前临时显示QChartView等等.但没能得到好的结果。
发布于 2017-04-19 05:06:19
我没有找到任何简单的方法,所以这里有我的解决方案,这更像是一个解决办法:
QPixmap ChartView::getChartPixmap()
{
QWidget* w = new QWidget; //creating a temporary widget, which will enable to display the chart
w->resize(REPORT_IMAGE_WIDTH, REPORT_IMAGE_HEIGHT);
QVBoxLayout *vl;
vl = new QVBoxLayout(w);
vl->addWidget(this); //'this' being the QChartView
w->show(); //showing the widget so it is resized and can be grabbed with the correct dimensions
QTest::qWait(500); //we need to wait for a little for the graph to be drawn otherwise you'll still have the same size problem
QPixmap pixmap = w->grab(); //retrieve the pixmap
w->hide(); //hiding the widget
return pixmap;
}
它正在工作,但你会有一个小窗口打开与图表500毫秒。
发布于 2017-03-27 04:52:56
我使用下面的代码在Pixmap上呈现一个QGraphivsView,因为QtCharts是基于QGraphivsView的,所以我认为这也是可行的。
尝试渲染图像,而不是试图抓取像素地图。
void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter *painter, QGraphivsView* profile)
{
int x = profilePlaceholder.x() - viewPort.x();
int y = profilePlaceholder.y() - viewPort.y();
QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height());
profile->render(painter, pos);
}
https://stackoverflow.com/questions/43046875
复制相似问题