QT Graphics场景-文本标签冲突

QT Graphics场景-文本标签冲突,qt,collision,qgraphicsscene,Qt,Collision,Qgraphicsscene,我希望停止标签在场景中发生碰撞,因此使用以下代码检查碰撞:- QGraphicsTextItem *textLabel = new QGraphicsTextItem; .... addItem(textLabel); //check for collision QList<QGraphicsItem*> items = this->items(textLabel>boundingRect(),Qt::IntersectsItemBoundingRect); QGr

我希望停止标签在场景中发生碰撞,因此使用以下代码检查碰撞:-

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

//check for collision
QList<QGraphicsItem*> items = this->items(textLabel>boundingRect(),Qt::IntersectsItemBoundingRect);
QGraphicsTextItem*textlab=新的QGraphicsTextItem;
....
附加项(文本标签);
//检查有无碰撞
QList items=this->items(textlab>boundingRect(),Qt::IntersectsItemBoundingRect);

我从来没有在列表中得到任何项目,但在屏幕上我可以看到冲突。我没有正确阅读文档吗?

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

但是,请注意,QGraphicsItem具有以下功能:-

QList<QGraphicsItem *> QGraphicsItem::collidingItems(Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList QGraphicsItem::collingitems(Qt::ItemSelectionMode=Qt::IntersectsItemShape)常量
如文件所述:-

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

所以你最好打电话:-

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

尝试调试
textlab>boundingRect()的结果。
。你可能会注意到位置总是一样的。CollingItems()正是我所需要的