Solved problem with "painter->drawImage" in paint() from QGraphicsRectItem
I try to draw on image on a QGraphicsScene and it doesn't work. I create an Object "MobileUnit::MobileUnit() : QGraphicsRectItem()", and this object should load an image but it doesn't work. I can draw a rectangle but not load an image and I don't understand why ?
Thank you for your help.
void MobileUnit::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
//work draw rectangle /*QPen pen(Qt::black, 3); painter->setPen(pen); QRectF rect=QRectF(0,0,70,70); painter->drawRect(rect);*/ //doesn't work painter->setPen(Qt::NoPen); QImage img; img.load("qrc:/../images/aircraft2.png"); QRectF r; r.setRect(0,0,img.width(),img.height()); painter->drawImage(r,img);
Hi and welcome to devnet,
That path looks suspicious.
Please take a look at the Qt Resources system documentation to see how the path are handled.
Thank you it works.
I didn't think about it, but yes apparently my path is broken. I change with :
img.load("qrc:/../images/aircraft2.png"); // doesn't work
and now it works. I feel stupid. I have a file "resources.qrc" that have the path to my image, why I cannot give the path this way "qrc:/../images/aircraft2.png" and I have to give the path this way "../images/aircraft2.png" ?
Because that path is wrong. You are referencing the images as if it was on your file system. That's the issue.