【问题标题】:Use one QPainter to paint multiple outputs at once: SVG and QImage使用一个 QPainter 一次绘制多个输出:SVG 和 QImage
【发布时间】:2015-03-15 15:36:23
【问题描述】:

我的 Qt 应用程序使用 QPainter 来绘制矢量图形。我需要这个图形输出两次,一次作为 SVG 格式的矢量输出,我使用 QSvgGenerator,一次作为像素格式,我使用 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

或使用两个不同的后端绘制两次:

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

这两种解决方案都有效,但对我来说似乎都非常低效,因为我总是两次绘制相同的场景。后者调用 QPainter 上的所有函数两次,前者通过重新跟踪我刚刚生成的 SVG 输出再次绘制所有操作。

有没有办法将多个后端附加到一个 QPainter 以仅绘制整个场景一次?

【问题讨论】:

    标签: c++ qt svg qpainter qimage


    【解决方案1】:

    我不认为你可以开箱即用地得到你想要的东西。您可以深入研究 Painter 的私有实现,并想出一种方法来一次性完成所有操作 - 生成每个矢量绘制器组件并将其光栅化到另一个绘制设备并移动到下一个绘制设备,但这可能并不简单,也很难做到值得。

    只需简要介绍您目前拥有的两种解决方案并坚持使用更快的一种,看起来第一种可能会更有效。

    【讨论】:

    • 正是我所担心的。还是谢谢。
    • @Philipp - 节省 CPU 时间的唯一方法是,您只能为每个图元的绘制创建一次几何图形,然后对 SVG 和光栅化使用相同的几何图形。但是,嘿,重复两次似乎是一种浪费——但如果它仍然足够快——谁在乎,它的机器在出汗,而不是你深入研究 Qt 的内部结构。
    • 来自文档:如果您需要绘制复杂的形状,特别是如果您需要重复执行此操作,请考虑创建一个 QPainterPath 并使用 drawPath() 进行绘制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多