【发布时间】:2017-08-07 18:42:48
【问题描述】:
我想将QChart(其核心是QGraphicsWidget)呈现给特定的画家,例如QSvgGenerator。
我已经阅读了以下主题 https://forum.qt.io/topic/38352/rendering-qgraphicsitem-without-qgraphicsscene/2 并在我的代码中实现了它:
QBuffer b;
QSvgGenerator p;
p.setOutputDevice(&b);
QSize s = app->chart()->size().toSize();
p.setSize(s);
p.setViewBox(QRect(0,0,s.width(),s.height()));
QPainter painter;
painter.begin(&p);
painter.setRenderHint(QPainter::Antialiasing);
app->chart()->paint(&painter, 0, 0); // This gives 0 items in 1 group
m_view->render(&painter); // m_view has app->chart() in it, and this one gives right image
qDebug() << "Copied";
painter.end();
QMimeData * d = new QMimeData();
d->setData("image/svg+xml",b.buffer());
QApplication::clipboard()->setMimeData(d,QClipboard::Clipboard);
cmets 有两行:第一行用于直接绘制QChart,第二行-渲染QGraphicsView。
我已经尝试过使用setViewBox,将其设置为巨大的值并没有帮助。如果我使用QImage 而不是QSvgGenerator,效果是一样的,我得到的是空白图片。
所以问题是为什么QChart->paint() 给我空画?
编辑:可以在 bitbucket 上找到工作代码:https://bitbucket.org/morodeer/charts_test_2/commits/b1eee99736beb5e43eae2a40ae116ee07e01558f
【问题讨论】: