前言:
这两天做程序的时候碰到的问题,两个QGraphicsItem和一个QGraphicsPixmapItem放到一个场景中QGraphicsPixmapItem作为背景,两个普通item为两个小方块,其中一个可以移动 我要的效果是当那个可以移动的方块与另一个方块碰撞时 可以检测出来然后输出。可是程序运行起来后发现两个方块还没有发生碰撞,但是确输出了发生碰撞的检测信息。经过检查 发现可能是因为方块与背景发生了重合导致的,因为背景QGraphicsPixmapItem也是作为一个item场景中,导致可移动方块item没有进行区分。于是找到了个函数可以完美解决这个问题
正文:
函数:
void QGraphicsItem::setToolTip(const QString &toolTip)
Sets the item's tool tip to toolTip. If toolTip is empty, the item's tool tip is cleared.
See also toolTip() and QToolTip.
通过上述函数可以将Item进行设置QString类型的提示。与之配合的函数还有:
QString QGraphicsItem::toolTip() const
Returns the item's tool tip, or an empty QString if no tool tip has been set.
See also setToolTip() and QToolTip.
QGraphicsItem::toolTip()会将给Item设置的提示以QString类型返回出来,这样就可以在程序中通过得到的item的指针来获取其提示信息,然后就能够进行判断 发生碰撞的是否是自己希望碰撞的。