QT GraphicScene - Text label collission

六月ゝ 毕业季﹏ 提交于 2019-12-12 01:45:43

问题


I want to stop labels colliding within my Scene and as a result used this code to check for a collision:-

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

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

I never get any items in the list, yet on screen I can see the collisions. Am I reading the documentation incorrectly?


回答1:


You're checking if any items are colliding with the label's bounding rect which is in the label's local coordinates. What you should be doing is checking relative to scene coordinates.

However, note that QGraphicsItem has this function: -

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

Which, as the documentation describes: -

Returns a list of all items that collide with this item. The way collisions are detected is determined by applying mode to items that are compared to this item, i.e., each item's shape or bounding rectangle is checked against this item's shape. The default value for mode is Qt::IntersectsItemShape.

So you'd be better calling: -

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


来源:https://stackoverflow.com/questions/20335233/qt-graphicscene-text-label-collission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!