Results 1 to 8 of 8

Thread: display .svg in QGraphicsView

  1. #1
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default display .svg in QGraphicsView

    Hi,

    how can i display a .svg in a QGraphicsView?

    i tried the following but nothing is displayed:
    Qt Code:
    1. // ui->gview is a QGraphicsView object
    2.  
    3. scene.addPixmap(QPixmap("/tmp/img/test.svg"));
    4. ui->gview->setScene(&scene);
    5. ui->gview->show();
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    S

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: display .svg in QGraphicsView

    Have you checked that the file was successfully loaded in the QPixmap? QPixmap::isNull() or QPixmap::load() are your friends.

    Anyway, a quick look at the documentation turned up QGraphicsSvgItem.

  3. #3
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display .svg in QGraphicsView

    the following code works now, but i dont know how to resize the svg img to the size of the QGraphicsScene...ideas? QGraphicsScene cannot display the entire svg, bc the svg is too large...
    Qt Code:
    1. scene->addItem(new QGraphicsSvgItem("/tmp/img/test.svg"));
    2. ui->gview->setScene(scene);
    3. ui->gview->show();
    To copy to clipboard, switch view to plain text mode 

    edit: i just saw from subforum...
    Last edited by Surfman19; 4th August 2015 at 12:17.

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: display .svg in QGraphicsView

    Since you do not explicitly set a scene rectangle to your QGraphicScene, it automatically grows to the size of the item.

    Do you want the view to display the whole item? Then another quick look at the documentation for QGraphicsView should let you know how to have the item fit in the view.

  5. #5
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display .svg in QGraphicsView

    yes, i want to get the entire svg visible...

    the following does not work... the svg is way too tiny now...

    Qt Code:
    1. QGraphicsSvgItem *item = new QGraphicsSvgItem("/tmp/img/test.svg");
    2. scene->addItem(item);
    3. ui->gview->setScene(scene);
    4. ui->gview->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
    5. ui->gview->show();
    6.  
    7. qDebug() << "ui->graphicsView->size(): " << ui->graphicsView->size();
    8. qDebug() << "scene->sceneRect().size(): " << scene->sceneRect().size();
    To copy to clipboard, switch view to plain text mode 

    svg size: 1039 x 745 pixel

    output:
    ui->graphicsView->size(): QSize(100, 30)
    scene->sceneRect().size(): QSizeF(831, 596)

    why is that?
    Last edited by Surfman19; 4th August 2015 at 14:21.

  6. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: display .svg in QGraphicsView

    There is no such thing as QGraphicsView::fitItem(). Your program cannot be compiled, so I am not surprised that it "does not work".

    The scene and view have separate coordinate systems. You can render different parts of a scene in several views.

  7. #7
    Join Date
    Mar 2009
    Posts
    34
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display .svg in QGraphicsView

    copy paste error... it should be: fitInView

    it compiles, but the size of the svg in the QGraphicsView is tiny....any idea?
    see here:
    img.jpg
    Last edited by Surfman19; 4th August 2015 at 14:23.

  8. #8
    Join Date
    Jan 2016
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: display .svg in QGraphicsView

    Hello,

    I have the same. My SVG is very tiny in the view after fitInView. Any Solution?

Similar Threads

  1. Replies: 5
    Last Post: 3rd March 2016, 16:42
  2. QGraphicsView (using int array) advice and display help
    By enricong in forum Qt Programming
    Replies: 4
    Last Post: 11th April 2012, 14:40
  3. Display an image in a QGraphicsview
    By emilio in forum Newbie
    Replies: 8
    Last Post: 6th December 2011, 07:55
  4. MDI display Item in QGraphicsView
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 01:11
  5. Display Images in QGraphicsView
    By robertaandrews in forum Qt Tools
    Replies: 4
    Last Post: 15th May 2007, 19:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.