How to use a png image as a QGraphicItem?
Solved
General and Desktop
-
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 ?
-
What type of image is that ? Again, it's really unusual for an image file to not have an extension.
-
It that case, check whether the QPixmap is null.