Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Showing an SVG on a GraphicsView

    General and Desktop
    2
    3
    100
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      Kyosaur last edited by

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

      }

      B 1 Reply Last reply Reply Quote 1
      • B
        Bonnie @Kyosaur last edited by Bonnie

        @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);
        
        K 1 Reply Last reply Reply Quote 4
        • B
          Bonnie @Kyosaur last edited by Bonnie

          @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);
          
          K 1 Reply Last reply Reply Quote 4
          • K
            Kyosaur @Bonnie last edited by

            @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!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post