【问题标题】:QT GraphicScene - Text label collissionQT 图形场景 - 文本标签碰撞
【发布时间】:2013-12-03 23:38:00
【问题描述】:

我想阻止标签在我的场景中发生碰撞,因此使用此代码检查碰撞:-

QGraphicsTextItem  *textLabel = new QGraphicsTextItem;
....
addItem(textLabel);

//check for collision
QList<QGraphicsItem*> items = this->items(textLabel>boundingRect(),Qt::IntersectsItemBoundingRect);

我从来没有在列表中找到任何项目,但在屏幕上我可以看到冲突。我是不是看错了文档?

【问题讨论】:

  • 尝试调试textLabel&gt;boundingRect()的结果。您可能会注意到位置始终相同。

标签: qt collision qgraphicsscene


【解决方案1】:

您正在检查是否有任何项目与位于标签本地坐标中的标签边界矩形发生冲突。您应该做的是检查相对于场景坐标。

但是,请注意 QGraphicsItem 有这个功能:-

QList<QGraphicsItem *> QGraphicsItem::collidingItems(Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

如文档所述:-

返回与此项目冲突的所有项目的列表。 检测碰撞的方式是通过将模式应用于与该项目进行比较的项目来确定的,即每个项目的形状或边界矩形都会根据该项目的形状进行检查。模式的默认值是 Qt::IntersectsItemShape。

所以你最好打电话给:-

QList<QGraphicsItem*> items = this->collidingItems();

【讨论】:

  • 完美。 collidingItems() 完全符合我的需要
猜你喜欢
  • 2018-11-07
  • 2023-03-06
  • 1970-01-01
  • 2015-08-14
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多