C++ QtChart-C++;-保存一个非';t显示
我正在尝试将图表保存到文件中,在本例中为QTextDocument:C++ QtChart-C++;-保存一个非';t显示,c++,qt,qtcharts,C++,Qt,Qtcharts,我正在尝试将图表保存到文件中,在本例中为QTextDocument: QTextDocument doc("Frame rate test\n"); QTextCursor cursor(&doc); cursor.movePosition(QTextCursor::End); if (getTestFinishedStatus()) { QPixmap pix = _pFrameRateChart->grab(); //_pFrameRateChart is QChar
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中显示图表,结果不一样。
以下是未显示时的结果:
以下是显示时的结果:
显然,我希望得到第二个结果,即使我没有将图表视图添加到小部件以在ui上显示它。
我尝试过调整QChartView的大小,调整QChart的大小,将图表添加到临时小部件和QVBoxLayout中,然后保存它,在保存它之前临时显示QChartView等等。。。但是没有得到好的结果。我使用以下代码在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);
}
我使用以下代码在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);
}
我没有找到任何简单的方法来解决这个问题,所以我的解决方案更像是一种变通方法:
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毫秒的图形。我没有找到任何简单的方法来实现这一点,因此我的解决方案更像是一个解决方案:
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毫秒的图形。不幸的是,我仍然得到了与大图相同的结果,但一张小图用我在实际项目中使用的一些代码更新了答案。不幸的是,我仍然得到与大图相同的结果,但一张小图用一些代码更新了答案我在实际项目中使用的代码。