SVG file in QGraphicsView
-
Hi, I'm wondering if I can put an SVG file into a QGraphicsView widget. I've seen something about QSvgRenderer, but I don't know if that would work.
I was using QPixmap when I was displaying jpg files, but svg files don't work. -
Hi,
QGraphicsSvgItem is what you are looking for.
-
Hi @SGaist, ok, but I don't know how to properly use it. I did this:
QGraphicsSvgItem svg(":/hands/handLeftDark.svg");
But I don't know how to add in in QGraphicsView now. I tried:
ui->graphicsView_LeftHand->scene()->addItem(svg);
But I don't know how to convert QGraphicsSvgItem to QGraphicsItem.
-
QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(":/hands/handLeftDark.svg"); ui->graphicsView_LeftHand->scene()->addItem(svgItem);
-
Are you linking the svg module ?
-
in pro
QT += svg
in cmake
find_package(Qt5 REQUIRED ... Svg ... ) -
Hi @JoeCFD, ok I just realised that I put it in a wrong thing in my .pro file, and when I put it right, it works. I don't know where the cmake file is, but it doesn't matter not because it works.
But I have another problem, you see, I have a dark and light mode in my app, and when I switch, QGraphicsView makes 2 lines on the edges of the image. I tried "ui->graphicsview->scene()->clear", but that just crashes the application. This is how the lines look:
-
override this one and define the size of your pic
QRectF boundingRect() const -
@JoeCFD, ok I tried that, but it doesn't seem to be working...
It doesn't display anything. Here's what it displays:
My code:QPixmap left(":/hands/handLeftDark.svg"); QRectF size(150, 150, 150, 150); ui->graphicsView_LeftHand->scene()->addPixmap(left); ui->graphicsView_LeftHand->setSceneRect(size);
-
Hi, so I solved it by doing "ui->graphicsView_LeftHand->scene()->removeItem(svgLeftDark);" and then adding the other svg.
So it's like this:QGraphicsScene *sceneLeft = new QGraphicsScene(this); ui->graphicsView_LeftHand->setScene(sceneLeft); QGraphicsSvgItem *add = new QGraphicsSvgItem("path/to/svg.svg"); QGraphicsSvgItem *remove = new QGraphicsSvgItem("path/to/svg.svg"); ui->graphicsView_LeftHand->scene()->removeItem(remove); //remove the svg that you don't want ui->graphicsView_LeftHand->scene()->addItem(add); //add the svg that you want
If you want to have it across different voids, just add this at the top of the .cpp file (where you declare cross void variables, ...):
QGraphicsSvgItem *add = new QGraphicsSvgItem("path/to/svg.svg"); QGraphicsSvgItem *remove = new QGraphicsSvgItem("path/to/svg.svg");
2/13