How to scale a QsvgGraphicsItem to the size of the QGraphicsView in which it is shown?
-
Hello,
I want to scale a svg to the size of a QGraphicsView Object.My Code Actually looks like this.
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QString> #include <QSvgRenderer> #include <QGraphicsSvgItem> #include "mysvgitem.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } class QAction; class QGraphicsView; class QGraphicsScene; class QGraphicsRectItem; class QLabel; class QGraphicsSvgItem; class QSvgRenderer; class QPainter; class MySvgItem; QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); bool loadFile(const QString &path); ~MainWindow(); private slots: void on_function1_windowIconTextChanged(const QString &iconText); private: Ui::MainWindow *ui; QGraphicsScene *scene1; QGraphicsScene *scene2; QGraphicsScene *scene3; QGraphicsScene *scene4; QGraphicsScene *scene5; QGraphicsScene *scene6; MySvgItem *f1; MySvgItem *f2; MySvgItem *f3; MySvgItem *f4; MySvgItem *f5; MySvgItem *f6; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDomDocument> #include <QFile> #include <QDebug> QString getChildId(QDomElement symbolElement) { QDomNodeList childList = symbolElement.childNodes(); if (childList.size() == 1) { qDebug() << qPrintable(childList.item(0).toElement().attributeNode("id").value() + "Found") << endl; return childList.item(0).toElement().attributeNode("id").value(); }else return childList.item(0).toElement().attributeNode("id").value(); qDebug() << qPrintable(childList.item(1).toElement().attributeNode("id").value() + "Found") << endl; } QDomElement getDomElement(QDomDocument doc, QString elementID) { QDomElement docElem = doc.documentElement(); QDomNode n = docElem.firstChild(); while(!n.isNull()) { QDomElement e = n.toElement(); // try to convert the node to an element. if(!e.isNull()) { QDomAttr id = e.attributeNode("id"); if (id.value().compare(elementID) == 0){ qDebug() << qPrintable(id.value() + "Found"); return e; } } n = n.nextSibling(); } return QDomElement(); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); static QColor POWERTRAIN_YELLOW = QColor(255, 255, 0); static QColor HYDRO_BLUE = QColor(0,0,255); static QColor LIGHT_GREEN = QColor(0,128,0); static QColor HEAT_BLACK = QColor(0,0,0); static QColor ORANGE = QColor(255,140,0); QDomDocument doc(QStringLiteral(":/assets/Icons.svg")); QFile file(QStringLiteral(":/assets/Icons.svg")); if (!file.open(QIODevice::ReadOnly)) return; if (!doc.setContent(&file)) { file.close(); return; } file.close(); // print out the element names of all elements that are direct children // of the outermost element. scene1 = new QGraphicsScene(this); scene2 = new QGraphicsScene(this); scene3 = new QGraphicsScene(this); scene4 = new QGraphicsScene(this); scene5 = new QGraphicsScene(this); scene6 = new QGraphicsScene(this); QSvgRenderer *renderer = new QSvgRenderer(QStringLiteral(":/assets/Icons.svg")); f1 = new MySvgItem(); f2 = new MySvgItem(); f3 = new MySvgItem(); f4 = new MySvgItem(); f5 = new MySvgItem(); f6 = new MySvgItem(); f1->setSize(80,80); ui->function1->setSceneRect(0,0,80,80); f1->setSharedRenderer(renderer); QDomElement f1DomElement = getDomElement(doc, QStringLiteral("SVGF1M1")); QString f1ChildId = getChildId(f1DomElement); f1->setElementId(f1ChildId); ui->function1->setScene(scene1); scene1->addItem(f1); ui->function1->show(); f2->setSharedRenderer(renderer); QDomElement f2DomElement = getDomElement(doc, QStringLiteral("SVGF28M1")); QString f2ChildId = getChildId(f2DomElement); f2->setElementId(f2ChildId); ui->function2->setScene(scene2); scene2->addItem(f2); ui->function2->show(); f3->setSharedRenderer(renderer); QDomElement f3DomElement = getDomElement(doc, QStringLiteral("SVGF42M1")); QString f3ChildId = getChildId(f3DomElement); f3->setElementId(f3ChildId); ui->function3->setScene(scene3); scene3->addItem(f3); ui->function3->show(); f4->setSharedRenderer(renderer); QDomElement f4DomElement = getDomElement(doc, QStringLiteral("SVGF10M1")); QString f4ChildId = getChildId(f4DomElement); f4->setElementId(f4ChildId); ui->function4->setScene(scene4); scene4->addItem(f4); ui->function4->show(); f5->setSharedRenderer(renderer); QDomElement f5DomElement = getDomElement(doc, QStringLiteral("SVGF17M1")); QString f5ChildId = getChildId(f5DomElement); f5->setElementId(f5ChildId); ui->function5->setScene(scene5); scene5->addItem(f5); ui->function5->show(); f6->setSharedRenderer(renderer); QDomElement f6DomElement = getDomElement(doc, QStringLiteral("SVGF20M1")); QString f6ChildId = getChildId(f6DomElement); f6->setElementId(f6ChildId); ui->function6->setScene(scene6); scene6->addItem(f6); ui->function6->show(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_function1_windowIconTextChanged(const QString &iconText) { }
Actually the Svg gets cut at some point. and doesnt show properly.
How do i display it correctly?
Cheers David
-
I don't know what you do in MySvgItem.
Do not set a size, and do not set a SceneRect. Instead, call QGraphicsView::fitInView on the SVG item. -
Thx for the reply, i did override the setSizefunction for the QGraphicsSvgItem, ths is not needed anymore with the fitInView function.
f1->setSharedRenderer(renderer); QDomElement f1DomElement = getDomElement(doc, QStringLiteral("SVGF1M1")); QString f1ChildId = getChildId(f1DomElement); f1->setElementId(f1ChildId); ui->function1->setScene(scene1); scene1->addItem(f1); ui->function1->fitInView(f1); ui->function1->show();
But if i use this, the svg is only gtting streched on the yaxis.
how would i fix this?
-
Thx alot! I somehow missed the tooltip about the aspectratio ^^
but one thing is strange:
if i set the Aspectratio to KeepAspectRatio it is getting smaller...
only if i set KeepAspectRatioByExpanding its getting bigger... -
Check the "defaultSize" property of your SVG via the QSvgRenderer. I assume this behavior is related to Qt believing that this SVG is supposed to be displayed in that size.
-
@Asperamanca
the defaultsize seems to be QSize(49,48)however i cannot figure out how to change this?
-
@gewitt
Hi
Did you check
https://doc.qt.io/qt-5/qsvgrenderer.html#viewBox-prop
and see what that reports ?