How to make a SVG at tab Widget ?
-
I make a tab Widget in the app.
Now I make a Graphics View like the following picture.
How could I add a SVG file at Graphic View ??
-
Hi,
One option is to use a QGraphicsPixmapItem.
-
-
@RiceBall said in How to make a SVG at tab Widget ?:
How could I code it ??
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
How could I code it ??
Sorry, I have tried it.
But I am not sure where is wrong.
I can't show anything.(MY SVG is animtion)
Can you help me check it ??QImage image1("C:/Users/shih.licheng/Documents/iHMI_2/splash1.svg"); QGraphicsPixmapItem itm1(QPixmap::fromImage(image1)); QGraphicsScene* scene = new QGraphicsScene; scene->addItem(&itm1); QImage image2("C:/Users/shih.licheng/Documents/iHMI_2/splash2.svg"); QGraphicsPixmapItem itm2(QPixmap::fromImage(image2)); scene->addItem(&itm2); QGraphicsView view(scene); view.show();
And I have another easy question.
if I use ui(graphics View).
How could I replace above "QGraphicsView view(scene)"
-
@RiceBall said in How to make a SVG at tab Widget ?:
QGraphicsView view(scene);
view.show();You're creating local QGraphicsView instance which is destroyed as soon as it goes out of scope (by the way you did not say where you actually have this code). Also check whether the paths are correct and what https://doc.qt.io/qt-5/qimage.html#isNull returns.
"How could I replace above "QGraphicsView view(scene)"" - just use the view from ui->?
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
QGraphicsView view(scene);
view.show();You're creating local QGraphicsView instance which is destroyed as soon as it goes out of scope (by the way you did not say where you actually have this code). Also check whether the paths are correct and what https://doc.qt.io/qt-5/qimage.html#isNull returns.
"How could I replace above "QGraphicsView view(scene)"" - just use the view from ui->?
- I use it at Mainwindow.
As you say, I can see it is shown suddenly then it is gone. - I use graphic View UI at mainwindow.ui.
How could I fix code about QGraphicsView view(scene)?
attachment is my code.
https://drive.google.com/open?id=17mOeeA11xWIo17LzSuDw6pcf9QvLePnH
- I use it at Mainwindow.
-
Hi
I assume you want to use the one you put in the tab ?QGraphicsView view(scene);
view.show();ui->graphicsView->setScene(scene);
-
@mrjj said in How to make a SVG at tab Widget ?:
Hi
I assume you want to use the one you put in the tab ?QGraphicsView view(scene);
view.show();ui->graphicsView->setScene(scene);
Thank you for your reply.
It looks like have loading something.But as you say , I think it just only show one moment.
Where is wrong?
-
-
@Devopia53 said in How to make a SVG at tab Widget ?:
like this:
graphicsView->scene()->addWidget(new QSvgWidget(":/your.svg"));
Thank you for your reply.
I try to fix it.
But it show following message.
-
-
@RiceBall
AddQT += svg
in .pro fileRefer Rendering SVG Files
-
@Devopia53
@Pradeep-P-N said in How to make a SVG at tab Widget ?:@RiceBall
AddQT += svg
in .pro fileRefer Rendering SVG Files
I try to fix it.
It almost pass all compile.
But it show "The process was ended forcefully."
-
@RiceBall said in How to make a SVG at tab Widget ?:
But it show "The process was ended forcefully."
Please debug and tell us where exactly it crashes...
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
But it show "The process was ended forcefully."
Please debug and tell us where exactly it crashes...
-
@RiceBall Please show the whole content of your MainWindow constructor, especially line 31.
I guess it is the one you posted in a screen-shot before? It would be helpful if you would simply copy/paste the code instead of posting screen-shots.
It looks like your scene was not yet initialised.
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall Please show the whole content of your MainWindow constructor, especially line 31.
I guess it is the one you posted in a screen-shot before? It would be helpful if you would simply copy/paste the code instead of posting screen-shots.
It looks like your scene was not yet initialised.I tried to simple it.
But the problem is the same..
mainwindow.cpp#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtGlobal> #include <QMovie> #include <QSvgWidget> #include <QPixmap> #include <QGraphicsScene> #include <QGraphicsPixmapItem> #include <QGraphicsView> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->graphicsView->scene()->addWidget(new QSvgWidget("../untitled6/splash1.svg")); } MainWindow::~MainWindow() { delete ui; }
-
@RiceBall Did you set the scene?
https://doc.qt.io/qt-5/qgraphicsview.html#setScene
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall Did you set the scene?
https://doc.qt.io/qt-5/qgraphicsview.html#setSceneNo.I don't set any scene.
Should I need to set it by " QGraphicsScene scene " at mainwindow.cpp ??? Or Not ?
-
@RiceBall Well https://doc.qt.io/qt-5/qgraphicsview.html#scene says
"Returns a pointer to the scene that is currently visualized in the view. If no scene is currently visualized, 0 is returned."
I would say you have to set a scene as shown here https://doc.qt.io/qt-5/qgraphicsview.htmlQGraphicsScene scene; scene.addText("Hello, world!"); QGraphicsView view(&scene); // use setScene() instead view.show();
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall Well https://doc.qt.io/qt-5/qgraphicsview.html#scene says
"Returns a pointer to the scene that is currently visualized in the view. If no scene is currently visualized, 0 is returned."
I would say you have to set a scene as shown here https://doc.qt.io/qt-5/qgraphicsview.htmlQGraphicsScene scene; scene.addText("Hello, world!"); QGraphicsView view(&scene); // use setScene() instead view.show();
I try to fix it as your recommend.
But it will shown "no viable conversion from 'QGraphicsScene' to ' QGraphicsScene *'"
Where should I need to change ?
-
@RiceBall said in How to make a SVG at tab Widget ?:
'QGraphicsScene' to ' QGraphicsScene *'"
Please try
ui->graphicView->setScene(&scene);
-
Hi
Please notice you are killing the scene very fast.
Make it a member of the class.
Should live in class so it dont go out of scope,
-
@RiceBall Please think about what you're doing instead of blindly copy/paste code snippets which meant to be simple examples.
First: don't create the scene on the stack!
Second: pass pointer to setScene (the error message actually tells you what is wrong).
-
@Pradeep-P-N said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
'QGraphicsScene' to ' QGraphicsScene *'"
Please try
ui->graphicView->setScene(&scene);
I success to show it finally.
Thanks @jsulm @Pradeep-P-N @mrjj help.Key Point
.proQT += svg
final code
QGraphicsScene *scene = new QGraphicsScene; scene->addWidget(new QSvgWidget("../untitled6/splash1.svg")); scene->addWidget(new QSvgWidget("../untitled6/splash2.svg")); ui->graphicsView->setScene(scene);
-
@RiceBall said in How to make a SVG at tab Widget ?:
@Pradeep-P-N said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
'QGraphicsScene' to ' QGraphicsScene *'"
Please try
ui->graphicView->setScene(&scene);
I success to show it finally.
Thanks @jsulm @Pradeep-P-N @mrjj help.Key Point
.proQT += svg
final code
QGraphicsScene *scene = new QGraphicsScene; scene->addWidget(new QSvgWidget("../untitled6/splash1.svg")); scene->addWidget(new QSvgWidget("../untitled6/splash2.svg")); ui->graphicsView->setScene(scene);
Hello every one
I meet a new easy problem.
How could I define the scene size & transparent....?
-
Hi @RiceBall
Scene size please check minimumRenderSize.
QWidget has an option to set the windowOpacity.
Below is sample code for defining the size and transparency...
QGraphicsView *gView = new QGraphicsView(this); QGraphicsScene *scene = new QGraphicsScene; QSvgWidget *svgWid = new QSvgWidget(":/Freesample.svg"); svgWid->setWindowOpacity(0.5); // Change as needed scene->addWidget(svgWid); gView->resize(350, 350); gView->setScene(scene);
Output:
Opacity : 0.5
Without Opacity:
All the best.
-
@Pradeep-P-N said in How to make a SVG at tab Widget ?:
Hi @RiceBall
Scene size please check minimumRenderSize.
QWidget has an option to set the windowOpacity.
Below is sample code for defining the size and transparency...
QGraphicsView *gView = new QGraphicsView(this); QGraphicsScene *scene = new QGraphicsScene; QSvgWidget *svgWid = new QSvgWidget(":/Freesample.svg"); svgWid->setWindowOpacity(0.5); // Change as needed scene->addWidget(svgWid); gView->resize(350, 350); gView->setScene(scene);
Output:
Opacity : 0.5
Without Opacity:
All the best.
Thank you for your answer.
I have some problem.- I can't loading several SVG. If I loading several file. It just only show last one.
- I use UI screen to set background-color:black. And I am sure my SVG is none background color.
But when I loading it . It always show white background.
Result
QSvgWidget * svgWid1 = new QSvgWidget("../iHMI_2/splash1.svg"); QSvgWidget * svgWid2 = new QSvgWidget("../iHMI_2/splash2.svg"); QSvgWidget * svgWid3 = new QSvgWidget("../iHMI_2/splash3.svg"); QSvgWidget * svgWid4 = new QSvgWidget("../iHMI_2/splash4.svg"); QGraphicsScene *scene = new QGraphicsScene; scene->addWidget(svgWid1); scene->addWidget(svgWid2); scene->addWidget(svgWid3); scene->addWidget(svgWid4); ui->graphicsView->setScene(scene);
-
@RiceBall said in How to make a SVG at tab Widget ?:
I can't loading several SVG. If I loading several file. It just only show last one.
Is the code at the bottom of your post the one you're using to load SVGs? Maybe they are simply all at same position, so you only see the last one?
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
I can't loading several SVG. If I loading several file. It just only show last one.
Is the code at the bottom of your post the one you're using to load SVGs? Maybe they are simply all at same position, so you only see the last one?
No. They are at different position.
It looks like below.
-
Hi @RiceBall
Can you please create theCustomWidget
inheriting fromQWidget
with Some Layout in it to align the SVG contents.// Please customize the Class as per your requirement. // I just provided some dummy sample code.
Example:
class CustomWidget : public QWidget
QGridLayout *glyt = new QGridLayout; QSvgWidget *svgWid1 = new QSvgWidget(":/Freesample.svg"); QSvgWidget *svgWid2 = new QSvgWidget(":/Freesample.svg"); QSvgWidget *svgWid3 = new QSvgWidget(":/Freesample.svg"); QSvgWidget *svgWid4 = new QSvgWidget(":/Freesample.svg"); glyt->addWidget(svgWid1); glyt->addWidget(svgWid2); glyt->addWidget(svgWid3); glyt->addWidget(svgWid4); this->setLayout(glyt);
Then use the
CustomWidget
asCustomWidget *csw = new CustomWidget; scene->addWidget(csw);
Output:
Might help you.
All the best.
-
@Pradeep-P-N said in How to make a SVG at tab Widget ?:
Hi @RiceBall
Can you please create theCustomWidget
inheriting fromQWidget
with Some Layout in it to align the SVG contents.// Please customize the Class as per your requirement. // I just provided some dummy sample code.
Example:
class CustomWidget : public QWidget
QGridLayout *glyt = new QGridLayout; QSvgWidget *svgWid1 = new QSvgWidget(":/Freesample.svg"); QSvgWidget *svgWid2 = new QSvgWidget(":/Freesample.svg"); QSvgWidget *svgWid3 = new QSvgWidget(":/Freesample.svg"); QSvgWidget *svgWid4 = new QSvgWidget(":/Freesample.svg"); glyt->addWidget(svgWid1); glyt->addWidget(svgWid2); glyt->addWidget(svgWid3); glyt->addWidget(svgWid4); this->setLayout(glyt);
Then use the
CustomWidget
asCustomWidget *csw = new CustomWidget; scene->addWidget(csw);
Output:
Might help you.
All the best.
Sorry, I never use that.
Your meaning is I create a new class at mainwindow.h ??class CustomWidget : public QWidget {}
where can I code it ?mainwindow.h or mainwindow.cpp
QGridLayout *glyt = new QGridLayout; QSvgWidget *svgWid1 = new QSvgWidget("../iHMI_2/splash1.svg"); QSvgWidget *svgWid2 = new QSvgWidget("../iHMI_2/splash2.svg"); QSvgWidget *svgWid3 = new QSvgWidget("../iHMI_2/splash3.svg"); QSvgWidget *svgWid4 = new QSvgWidget("../iHMI_2/splash4.svg"); glyt->addWidget(svgWid1); glyt->addWidget(svgWid2); glyt->addWidget(svgWid3); glyt->addWidget(svgWid4); this->setLayout(glyt);
-
@RiceBall said in How to make a SVG at tab Widget ?:
where can I code it ?mainwindow.h or mainwindow.cpp
in customwidget.h + customwidget.cpp...
-
@jsulm said in How to make a SVG at tab Widget ?:
@RiceBall said in How to make a SVG at tab Widget ?:
where can I code it ?mainwindow.h or mainwindow.cpp
in customwidget.h + customwidget.cpp...
So I create new customwidget.h & customwidget.cpp
And coding like below.
Is it right ??
Because at mainwindow.cpp it show "use of undeclared identifier 'scene'".customwidget.h
#ifndef CUSTOMWIDGET_H #define CUSTOMWIDGET_H #include <QWidget> class customwidget : public QWidget { Q_OBJECT public: explicit customwidget(QWidget *parent = nullptr); signals: public slots: }; #endif // CUSTOMWIDGET_H
customwidget.cpp
#include "customwidget.h" #include <QGridLayout> #include <QSvgWidget> customwidget::customwidget(QWidget *parent) : QWidget(parent) { QGridLayout *glyt = new QGridLayout; QSvgWidget *svgWid1 = new QSvgWidget("../iHMI_2/splash1.svg"); QSvgWidget *svgWid2 = new QSvgWidget("../iHMI_2/splash2.svg"); QSvgWidget *svgWid3 = new QSvgWidget("../iHMI_2/splash3.svg"); QSvgWidget *svgWid4 = new QSvgWidget("../iHMI_2/splash4.svg"); glyt->addWidget(svgWid1); glyt->addWidget(svgWid2); glyt->addWidget(svgWid3); glyt->addWidget(svgWid4); this->setLayout(glyt); }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> #include "customwidget.h" //#include <QGraphicsScene> //#include <QGraphicsView> //#include <QtSvg/QSvgWidget> //#include <QMovie> //#include <QPixmap> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); CustomWidget *csw = new CustomWidget; scene->addWidget(csw);
-
@Pradeep-P-N
Is this your meaning like above reply?
-
@RiceBall
Hi
Yes, he meant to make such a custom class.However, in MainWindow, you just use a scene which is undefined. (or did u add it as member ?)
You should at least do
QGraphicsScene *scene = new QGraphicsScene;
first.
-
Hi @RiceBall
Sorry was bit busy, Yes follow below instructions.- Create a Class inheriting from
QWidget
(Example as above codeclass CustomWidget : public QWidget
) - Use the QLayout Class options available (Use whichever suitable to your UI Arrangement)
- Once you have the
CustomeWidget
ready you can use this Class as the widget for Graphics Scene
QGraphicsScene *scene = new QGraphicsScene CustomWidget *csw = new CustomWidget; scene->addWidget(csw);
- Optimize your code and you can achieve the result you are looking for.
All the best.
- Create a Class inheriting from
-
@mrjj
@Pradeep-P-N said in How to make a SVG at tab Widget ?:QGraphicsScene *scene = new QGraphicsScene
I try to fix code and test it.
But I can see it loading something but is white.ui->setupUi(this); CustomWidget *csw = new CustomWidget; QGraphicsScene *scene = new QGraphicsScene; scene->addWidget(csw); ui->graphicsView->setScene(scene);
-
@RiceBall said in How to make a SVG at tab Widget ?:
QSvgWidget *svgWid1 = new QSvgWidget("../iHMI_2/splash1.svg");
QSvgWidget *svgWid2 = new QSvgWidget("../iHMI_2/splash2.svg");
QSvgWidget *svgWid3 = new QSvgWidget("../iHMI_2/splash3.svg");
QSvgWidget *svgWid4 = new QSvgWidget("../iHMI_2/splash4.svg");You're using relative paths: did you make sure the files are actually found at runtime?