How to use SVG
-
Hi
Here is fast sample to get you going
https://www.dropbox.com/s/y9uy5wbnzicuqqb/mysvg.zip?dl=0whoops. i btter add some comments :)
QSvgWidget *svg = new QSvgWidget(this); // make the svg widget svg->load(QString(":/NewTux.svg")); // load svg file. make sure to change path in your app auto verticalLayout = new QVBoxLayout(centralWidget()); // make a layout to put the svgwidget it so it scales with the window verticalLayout->setContentsMargins(0, 0, 0, 0); // no borders verticalLayout->addWidget(svg);// the widget to it centralWidget()->setLayout(verticalLayout); // assign the layout to the mainwindow MainWindow is special as its center is actually another widget called central widget you gain access to with centralWidget() which we use instead of directly MainWindow it self. This can be confusing at first. Just think of it as a place holder widget MainWindow has.
-
One question, I have done everything to the best of my abilities but I still get the error:
"undefined reference to _imp_ZN10QSvgWidgetC1EP7QWidget"
"undefined reference to _imp_ZN10QSvgWidget4loadERK7QString"Here is my code:
qmake .pro:
QT += core gui QT += sql greaterThan(QT_MAJOR_VERSION, 4): QT += widgets svg CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target RESOURCES += \ Resources.qrc
mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSvgWidgets/QSvgWidget> #include <QtSvg> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QSvgWidget* svg = new QSvgWidget(this); svg->load(QString(":/images/images/Dollar.svg")); ui->verticalLayout_3->addWidget(svg); } MainWindow::~MainWindow() { delete ui; }
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif
main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
(PS: I tried to add [include<QtSvgWidget>] but it doesn't exist by it's own. It is rather inside the "QtSvgWidgtes/QtSvgWidget")
-
-
Which version of Qt are you currently using ?
-
Qt version 4.14.2 (That is what it says in the "About")
I don't understand something, I just recently downloaded Qt. When I downloaded Qt I selected the Qt 6 version but now as you told me to look at the version it says 4.14,2. What is going on I am very confused. Did I make a mistake in the download process?
-
No that's the Qt Creator version. There's no Qt 4.14.
You have to take a look at the kit you are using.
-
It was introduced from 4.1.
https://doc.qt.io/qt-5/qsvgwidget.html
QT += core gui widgets svg <=== you need widgets as well for qsvgwidgetIf you simply need a QPixmap from a svg source for other widgets, use QSvgRenderer to paint one.
-
Drop the C++ CONFIG, you do not need that currently.
You should also consider updating to the latest Qt 6.1.
-
-
Hi
Else a sure bet is installing Qt5.15.
While v6 does offer new things
https://wiki.qt.io/New_Features_in_Qt_6.0
Its mostly if you are using QML and it still has lots of features missing planned
for some later releases.