c++ - 如何在QT中打开svg文件(QGraphicsView)

标签 c++ qt qgraphicsview qtgui qtsvg

如何打开像png文件一样的svg文件。 例如它适用于 png 文件:

scene=new QGraphicsScene(QRect(10, 10, 680, 520));  
view=new QGraphicsView(this);
image=new QGraphicsPixmapItem(QPixmap("example.png"));

scene ->addItem(image);   
view ->setScene(scene);   
view ->setGeometry(QRect(270, 35, 700, 540));

有什么想法吗?

最佳答案

根据我的评论,您可以按照 documentation 使用 QGraphicsSvgItem :

QGraphicsSvgItem::QGraphicsSvgItem(const QString & fileName, QGraphicsItem * parent = 0)

Constructs a new item with the given parent and loads the contents of the SVG file with the specified fileName.

所以,你基本上会写如下内容:

 QGraphicsSvgItem *item = new QGraphicsSvgItem("example.svg");

如果您希望将它与 QSvgRenderer 一起使用,您也可以按照文档中的示例进行操作。

QSvgRenderer *renderer = new QSvgRenderer(QStringLiteral("example.svg"));
QGraphicsSvgItem *item = new QGraphicsSvgItem();

item->setSharedRenderer(renderer);
item->setElementId(QStringLiteral("example"));

在这里你甚至可以找到一个更复杂的例子:

SVG Viewer Example

关于c++ - 如何在QT中打开svg文件(QGraphicsView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099276/

相关文章:

qt - 如何使 QGraphicsEllipsitem 边框透明?

c++ - 分配给ostream后如何关闭ofstream?

c++ - 从 C++ 中的错误模板实例化继承

c++ - 如何在注销时优雅地退出 QApplication?

c++ - 使用 QT 库的 C++ 中这行代码的等效项是什么?

c++ - 拖动 View 时 QGraphicsItem 消失

c++ - 编译器对循环进行向量化

c++ - 错误 LNK2019 : unresolved external symbol error?

linux - Linux下使用Qt QDesktopServices::storageLocation的应用程序数据文件夹

qt - 在 Qt 图形 View 中获取固定大小项目的边界框