I made a custom QGraphicsItem and I am overriding the paint function to draw image which is ghost.png, a character that has a complex boundary, and can be moved by clicking on lower/upper/ left/right side of the image.
I understand this is happening because of the boundingRect, I am setting it larger because I need to draw something else, for example: text, health bar, etc...
How can I keep a larger boundingrect and also limit the image to move only if clicked on the image.
My class :
class DrawImage : public QGraphicsItem
{
public:
DrawImage();
protected:
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};
My Definition :
DrawImage::DrawImage()
{
setFlag(ItemIsMovable);
}
QRectF DrawImage::boundingRect() const
{
return QRectF(0, 0, 100, 200);
}
void DrawImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
auto ghost = QImage(":ghost.png");
painter->drawImage(QRect(0, 0, 100, 100), ghost);
}
musicamante said:
Here's how you can make your
QGraphicsPixmapItemmoveable only when clicked on itspixmap, and have aboundingRectaround it:Here's the result:
See the source code for more details: