Qt SVG module


SVG(scalable vector graphics可縮放矢量圖形)是基於可拓展標記語言(XML),用於描述二維矢量圖形的一種圖形格式。由W3C制定。是一種和圖像分辨率無關的矢量圖形格式。它與其他的圖像格式相比優勢在於:可以通過文本編輯器創建修改;可以被搜索、索引、腳本化或壓縮;可伸縮;可以被任何分辨率下高清晰打印;可在圖像質量不下降的情況下被放大。

 

使用QT SVG模塊需要在.pro文件里添加QT += SVG 

 

這里簡單介紹下Qt SVG的四個主要的類:

 

QGraphicsSvgItem

QGraphicsItem that can be used to render the contents of SVG files

QSvgGenerator

Paint device that is used to create SVG drawings

QSvgRenderer

Used to draw the contents of SVG files onto paint devices

QSvgWidget

Widget that is used to display the contents of Scalable Vector Graphics (SVG) files

 

QGraphicsSvgItem是QGraphicsObject的子類,它用來渲染SVG文件的內容。

QSvgGenerator是QPaintDevice的子類,它是用來繪制SVG的paint device。

QSvgRenderer是QObject的子類,它是用來畫SVG文件內容到paint device的。

QSvgWidget是QWidget的子類,它是用來展示svg文件內容的窗體部件。

 

 

QGraphicsSvgItem提供一個方法將SVG文件渲染到QGraphicsView。QGraphicsSvgItem可以通過傳遞SVG文件到它的構造函數,或者顯式設置shared QSvgRenderer到它上面。例如下面例子:

 

     QSvgRenderer *renderer = new QSvgRenderer(QLatin1String("SvgCardDeck.svg"));

     QGraphicsSvgItem *black = new QGraphicsSvgItem();

     QGraphicsSvgItem *red   = new QGraphicsSvgItem();

 

     black->setSharedRenderer(renderer);

     black->setElementId(QLatin1String("black_joker"));

 

     red->setSharedRenderer(renderer);

     red->setElementId(QLatin1String("red_joker"));

 

QGraphicsSvgItem提供了一個方法setElementId,只渲染被調用的SVG元素(及孩子元素)。

 

 

QSvgGenerator像一個QPrinter,它是個只寫的設備,為了輸出專門的格式的內容。為了寫出SVG文件,需要先指定名稱fileName或outputDevice屬性。一般要設置size屬性,有些情況需要viewBox屬性。例如下面例子:

 

     QSvgGenerator generator;

     generator.setFileName(path);

     generator.setSize(QSize(200, 200));

     generator.setViewBox(QRect(0, 0, 200, 200));

     generator.setTitle(tr("SVG Generator Example Drawing"));

     generator.setDescription(tr("An SVG drawing created by the SVG Generator "

                                 "Example provided with Qt."));

 

     QPainter painter;

     painter.begin(&generator);

     ...

     painter.end();

 

 

使用QSvgRenderer可以渲染文件內容到任意QpaintDevice子類,包括QWidget, QImage, QGLWidget。

 

對於動畫SVG繪制,可以使用animated()函數指明是否包含動畫信息;使用framesPerSecond設置獲取幀率。

 

最后,QSvgRenderer提供repaintNeeded()信號,當渲染文件需要被更新時發射。

轉載處:http://blog.csdn.net/xuguangsoft/article/details/8577626


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM