首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >QtChart - C++ -保存未显示的图表

QtChart - C++ -保存未显示的图表
EN

Stack Overflow用户
提问于 2017-03-27 12:45:59
回答 2查看 1K关注 0票数 1

我试图在本例中的QTextDocument中将图表保存到文件中:

代码语言:javascript
运行
复制
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等等.但没能得到好的结果。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-19 13:06:19

我没有找到任何简单的方法,所以这里有我的解决方案,这更像是一个解决办法:

代码语言:javascript
运行
复制
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毫秒。

票数 0
EN

Stack Overflow用户

发布于 2017-03-27 12:52:56

我使用下面的代码在Pixmap上呈现一个QGraphivsView,因为QtCharts是基于QGraphivsView的,所以我认为这也是可行的。

尝试渲染图像,而不是试图抓取像素地图。

代码语言:javascript
运行
复制
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);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43046875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档