#include <QApplication>#include <QGraphicsView>#include <QGraphicsScene>#include <QGraphicsItem>#include <QGraphicsRectItem>#include <QDebug>class MyCursor : public QGraphicsRectItem{public: MyCursor(QGraphicsItem *_parent = 0) : QGraphicsRectItem(0, 0, 40, 15, _parent) { } ~MyCursor() { } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) { QList<QGraphicsItem *> colliding = collidingItems(); QString str = QString("coll: ") + QString::number(colliding.size()); painter->setPen(QPen(QColor(Qt::black), 2)); painter->drawText(5, 12, str); painter->setPen(QPen(QColor(Qt::darkGreen), 2)); painter->drawRect(rect()); }};int main(int argc, char *argv[]){ QApplication a(argc, argv); QGraphicsScene *my_scene = new QGraphicsScene(QRectF(-100, -100, 400, 300)); QGraphicsView *my_view = new QGraphicsView(my_scene); QGraphicsRectItem *redFrame = new QGraphicsRectItem(QRectF(0, 0, 250, 120)); redFrame->setPen(QPen(QColor(Qt::red), 2)); redFrame->setPos(10, 10); //redFrame->setFlag(QGraphicsItem::ItemClipsChildrenToShape); my_scene->addItem(redFrame); QGraphicsRectItem *blueRect = new QGraphicsRectItem(QRectF(-40, -40, 80, 80), redFrame); blueRect->setPen(QPen(QColor(Qt::blue), 2)); blueRect->setFlag(QGraphicsItem::ItemIsMovable); MyCursor *greenCursor = new MyCursor(); greenCursor->setFlag(QGraphicsItem::ItemIsMovable); greenCursor->setPos(135, 160); my_scene->addItem(greenCursor); my_view->show(); return a.exec();}