Solved How to use a png image as a QGraphicItem?
-
How can I do this?I don't want to use an allipse or a circle or a rect
-
Hi
what about
https://doc.qt.io/qt-5/qgraphicspixmapitem.html
that would load png. -
Oh yeah! But it gives me an error
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("C:/Users/ILHA4/Desktop/trafo"), &scene);
It says:
"no matching constructor for initialization of 'QGraphicsPixmapItem'"UPDATE:
dialog.h
#include <QGraphicsPixmapItem> namespace Ui { class Dialog; } class Dialog : public QDialog { ... QGraphicsPixmapItem *item; };
dialog.cpp
#include <QGraphicsPixmapItem> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); scene= new QGraphicsScene(this); ui->graphicsView->setScene(scene); QBrush redBrush(Qt::red); QPen blackpen(Qt::black); item->setPixmap(QPixmap("C:/Users/ILHA4/Desktop/trafo")); scene->addItem(item); ellipse=scene->addEllipse(10,10,100,100,blackpen,redBrush); ellipse->setFlag(QGraphicsItem::ItemIsMovable); }
When i do this the program unexpectedly finishes :(
UPDATE 2:
dialog.cpp
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) {... QGraphicsPixmapItem *item=scene->addPixmap(QPixmap("C:/Users/ILHA4/Desktop/trafo")); item->setPos(50,50); scene->addItem(item); }
It doesn't give me an error, but the item doesn't appear on the screen :(
-
Hi,
@frnklu20 said in How to use a png image as a QGraphicItem?:
"C:/Users/ILHA4/Desktop/trafo"
That's pretty unusual for an image name. The extension is missing. Is that on purpose ?
-
thats the path of the image, where it is located on my computer
the name of the image is trafo, of high voltage transformer.
-
What type of image is that ? Again, it's really unusual for an image file to not have an extension.
-
it's a png image, but if i do:
GraphicsPixmapItem *item=scene->addPixmap(QPixmap("C:/Users/ILHA4/Desktop/trafo.png"));
it doesn't work either, the image doesn't appear on the screen
-
It that case, check whether the QPixmap is null.
-
Yes that's it, the path was wrong
Thanks man, you're awesome!