簡體   English   中英

使用一個QPainter一次繪制多個輸出:SVG和QImage

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

我的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來僅繪制一次整個場景?

我認為您無法立即獲得想要的東西。 您可以研究畫家的私有實現,並想出一種方法來一次完成所有操作-生成每個矢量畫家組件並將其柵格化到另一個繪畫設備,然后再移動到下一個繪畫設備,但這可能並不簡單,也很難做到值得。

只需概要介紹到目前為止的兩個解決方案,並堅持使用較快的解決方案,看起來第一個解決方案可能會更高效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM