Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Showing an SVG on a GraphicsView



  • 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);
    
    
    }
    

    }



  • @Kyosaur

    1. 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.
    2. 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);
    


  • @Kyosaur

    1. 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.
    2. 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);
    


  • @Bonnie OMG! That setElementId was the issue!!!! You are amazing. I was following someone's example on that line, and didn't fully understand what it did to be honest. Thank you for your help :D!


Log in to reply