Showing an SVG on a GraphicsView
Solved
General and Desktop
-
Hello everyone. Im really struggling on displaying an SVG file onto a QgraphicsView. I downloaded the examples, and they just didn't work. I feel a little out of my depth here. This is my code i've been experimenting with (code tag doesnt seem to work, so i'll paste it as is here). What am i doing wrong?
void ItemViewDialog::on_pushButton_2_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),tr("Cut file (*.SVG)"));if(!fileName.isNull() && !fileName.isEmpty()) { QSvgRenderer *renderer = new QSvgRenderer(fileName); QGraphicsSvgItem *item = new QGraphicsSvgItem(); if(!renderer->isValid()) { qDebug("INVALID SVG"); } item->setSharedRenderer(renderer); item->setElementId(QStringLiteral("example")); QGraphicsScene* scene = new QGraphicsScene(this); ui->graphicsView_2->setScene(scene); scene->addItem(item); }
}
-
- Do your svg has a element whose id is "example" and you only want to show that element? If not, do not add that
setElementId
line. - If you are not using a same svg file in multiple items, you don't need to use
setSharedRenderer
, just
QGraphicsSvgItem *item = new QGraphicsSvgItem(fileName);
- Do your svg has a element whose id is "example" and you only want to show that element? If not, do not add that