简体   繁体   English

使用一个QPainter一次绘制多个输出:SVG和QImage

[英]Use one QPainter to paint multiple outputs at once: SVG and QImage

My Qt application uses a QPainter to draw a vector graphic. 我的Qt应用程序使用QPainter绘制矢量图形。 I need this graphic output twice, once as a vector output in SVG format, where I'm using QSvgGenerator, and once as a pixel format, where I'm using QImage. 我需要两次此图形输出,一次是作为SVG格式的矢量输出(在此我使用QSvgGenerator),一次是一个像素格式(在此我使用QImage)。 According to what I've found in the documentation I can either first paint to SVG and then convert the SVG output to a Qimage: 根据我在文档中找到的内容,我可以先将其绘制为SVG,然后将SVG输出转换为Qimage:

QPainter painter;
QSvgGenerator generator;
generator.setSize(QSize(width_, height_));
// more initializations here
painter.begin(&generator);
doPaintMyStuff(&painter);
painter.end();
generator.setOutputDevice(...)   // pipe the SVG output to the server
QImage image(width_, height_, QImage::Format_ARGB32_Premultiplied);
QSvgRenderer renderer;
renderer.load(...)                // get the svg output we just generated
painter.begin(&image);
renderer.render(&painter);       // render the vector graphic to pixel
painter.end();
usePixelData(image.constBits()); // pipe the pixel output to the server

or draw twice using two different backends: 或使用两个不同的后端绘制两次:

QPainter painter;
QSvgGenerator generator;
generator.setSize(QSize(width_, height_));
// more initializations here
QImage image(width_, height_, QImage::Format_ARGB32_Premultiplied);
painter.begin(&generator);
doPaintMyStuff(&painter);
painter.end();
painter.begin(&image);
doPaintMyStuff(&painter);
painter.end();
generator.setOutputDevice(...)   // pipe the SVG output to the server
usePixelData(image.constBits()); // pipe the pixel output to the server

Both solutions work, but both seem terribly inefficient to me, since I'm always drawing the same scene twice. 两种解决方案都可以使用,但是对我来说似乎效率都非常低,因为我总是将同一场景绘制两次。 The latter calls all functions on QPainter twice, the former draws all operations again by re-tracing the SVG ouput I just generated. 后者调用QPainter上的所有函数两次,前者通过重新跟踪我刚刚生成的SVG输出来再次绘制所有操作。

Is there a way to attach multiple backends to one QPainter to paint the whole scene only once? 有没有一种方法可以将多个后端连接到一个QPainter来仅绘制一次整个场景?

I don't think you can get what you want out of the box. 我认为您无法立即获得想要的东西。 You could dig into the private implementation of the painter and come up with a way to do everything just once - generate each vector painter component and rasterize it to another paint device and move onto the next, but it will probably not be simple and hardly be worth it. 您可以研究画家的私有实现,并想出一种方法来一次完成所有操作-生成每个矢量画家组件并将其栅格化到另一个绘画设备,然后再移动到下一个绘画设备,但这可能并不简单,也很难做到值得。

Just profile the two solutions you have so far and stick to the faster one, looks like the first one might be a tad more efficient. 只需概要介绍到目前为止的两个解决方案,并坚持使用较快的解决方案,看起来第一个解决方案可能会更高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM