Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Showing an SVG on a GraphicsView
QtWS25 Last Chance

Showing an SVG on a GraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 245 Views
  • 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 Offline
    K Offline
    Kyosaur
    wrote on last edited by
    #1

    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
    1
    • K Kyosaur

      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 Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      @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
      4
      • B 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 Offline
        K Offline
        Kyosaur
        wrote on last edited by
        #3

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

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved