PDA

View Full Version : display .svg in QGraphicsView



Surfman19
4th August 2015, 10:46
Hi,

how can i display a .svg in a QGraphicsView?

i tried the following but nothing is displayed:


// ui->gview is a QGraphicsView object

QGraphicsScene scene;
scene.addPixmap(QPixmap("/tmp/img/test.svg"));
ui->gview->setScene(&scene);
ui->gview->show();


Thanks,
S

yeye_olive
4th August 2015, 10:55
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.

Surfman19
4th August 2015, 11:02
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...


QGraphicsScene *scene = new QGraphicsScene();
scene->addItem(new QGraphicsSvgItem("/tmp/img/test.svg"));
ui->gview->setScene(scene);
ui->gview->show();


edit: i just saw from subforum...

yeye_olive
4th August 2015, 11:17
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.

Surfman19
4th August 2015, 11:28
yes, i want to get the entire svg visible...

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



QGraphicsScene *scene = new QGraphicsScene();
QGraphicsSvgItem *item = new QGraphicsSvgItem("/tmp/img/test.svg");
scene->addItem(item);
ui->gview->setScene(scene);
ui->gview->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
ui->gview->show();

qDebug() << "ui->graphicsView->size(): " << ui->graphicsView->size();
qDebug() << "scene->sceneRect().size(): " << scene->sceneRect().size();


svg size: 1039 x 745 pixel

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

why is that?

yeye_olive
4th August 2015, 13:02
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.

Surfman19
4th August 2015, 13:14
copy paste error... it should be: fitInView

it compiles, but the size of the svg in the QGraphicsView is tiny....any idea?
see here:
11303

m-obi
13th May 2017, 21:53
Hello,

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