如何在QGraphicsItem的可移动区域周围绘制一个边界矩形?

huangapple go评论117阅读模式
英文:

How to draw a bounding rect around movable area of QGraphicsItem?

问题

我创建了一个自定义的QGraphicsItem,并重写了paint函数来绘制图像,这个图像是ghost.png,是一个具有复杂边界的角色,可以通过点击图像的///四个边来移动。

我理解这是因为boundingRect的设置,我将其设置得更大,因为我需要绘制其他内容,比如文本、生命值条等等...

如何保持较大的boundingRect,同时限制只有在图像上点击时才能移动图像?

我的类:

  1. class DrawImage : public QGraphicsItem
  2. {
  3. public:
  4. DrawImage();
  5. protected:
  6. QRectF boundingRect() const override;
  7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  8. };

我的定义:

  1. DrawImage::DrawImage()
  2. {
  3. setFlag(ItemIsMovable);
  4. }
  5. QRectF DrawImage::boundingRect() const
  6. {
  7. return QRectF(0, 0, 100, 200);
  8. }
  9. void DrawImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  10. {
  11. auto ghost = QImage("ghost.png");
  12. painter->drawImage(QRect(0, 0, 100, 100), ghost);
  13. }
英文:

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 :

  1. class DrawImage : public QGraphicsItem
  2. {
  3. public:
  4. DrawImage();
  5. protected:
  6. QRectF boundingRect() const override;
  7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  8. };

My Definition :

  1. DrawImage::DrawImage()
  2. {
  3. setFlag(ItemIsMovable);
  4. }
  5. QRectF DrawImage::boundingRect() const
  6. {
  7. return QRectF(0, 0, 100, 200);
  8. }
  9. void DrawImage::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  10. {
  11. auto ghost = QImage(":ghost.png");
  12. painter->drawImage(QRect(0, 0, 100, 100), ghost);
  13. }

答案1

得分: 2

musicamante 说道:

>重写 [QGraphicsItem::]shape

以下是如何使你的 QGraphicsPixmapItem 仅在点击其 pixmap 时可移动,并在其周围有一个 boundingRect 的示例:

  1. class DrawImage : public QGraphicsItem
  2. {
  3. public:
  4. DrawImage()
  5. {
  6. setFlag(ItemIsMovable);
  7. }
  8. protected:
  9. QRectF boundingRect() const override
  10. {
  11. return QRectF(0, 0, 100, 200);
  12. }
  13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
  14. {
  15. auto greenBall = QPixmap("green.svg");
  16. QRect rect = boundingRect().toRect();
  17. // 你可以调整 pixmap 的绘制位置
  18. // 我选择了 bounding rect 的上半部分
  19. rect.adjust(0,0,0,-rect.height()/2);
  20. painter->drawPixmap(rect, greenBall);
  21. // 为了可见性,我绘制了整个 bounding rect
  22. painter->setPen(Qt::green);
  23. painter->drawRect(boundingRect());
  24. }
  25. QPainterPath shape() const
  26. {
  27. // 这里你可以指定用户点击哪个区域移动对象
  28. auto greenBall = QPixmap("green.svg");
  29. QRect rect = boundingRect().toRect();
  30. // 确保它与你绘制 pixmap 的区域相同
  31. rect.adjust(0,0,0,-rect.height()/2);
  32. QPainterPath path;
  33. // 将其添加为你想要的形状,我选择了绿色圆圈的椭圆形状
  34. path.addEllipse(rect);
  35. return path;
  36. }
  37. };

这是结果图:

如何在QGraphicsItem的可移动区域周围绘制一个边界矩形?

查看源代码以获取更多详细信息:

英文:

musicamante said:

>Override [QGraphicsItem::]shape.

Here's how you can make your QGraphicsPixmapItem moveable only when clicked on its pixmap, and have a boundingRect around it:

  1. class DrawImage : public QGraphicsItem
  2. {
  3. public:
  4. DrawImage()
  5. {
  6. setFlag(ItemIsMovable);
  7. }
  8. protected:
  9. QRectF boundingRect() const override
  10. {
  11. return QRectF(0, 0, 100, 200);
  12. }
  13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
  14. {
  15. auto greenBall = QPixmap(":green.svg");
  16. QRect rect = boundingRect().toRect();
  17. //You can adjust where your pixmap is drawn
  18. //I chose the upper part of the bounding rect
  19. rect.adjust(0,0,0,-rect.height()/2);
  20. painter->drawPixmap(rect, greenBall);
  21. //I'm drawing the full bounding rect just to make it visible
  22. painter->setPen(Qt::green);
  23. painter->drawRect(boundingRect());
  24. }
  25. QPainterPath shape() const
  26. {
  27. //this is where you sepecify which area of your object allows the user to move it when clicked
  28. auto greenBall = QPixmap(":green.svg");
  29. QRect rect = boundingRect().toRect();
  30. //Make sure it is the same area where you're drawing your pixmap
  31. rect.adjust(0,0,0,-rect.height()/2);
  32. QPainterPath path;
  33. //add it as the shape you want, I chose an ellipse for the green circle
  34. path.addEllipse(rect);
  35. return path;
  36. }
  37. };

Here's the result:

如何在QGraphicsItem的可移动区域周围绘制一个边界矩形?

See the source code for more details:

huangapple
  • 本文由 发表于 2023年6月12日 02:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定