C++ (Qt)#include <QApplication>#include <QGraphicsScene>#include <QGraphicsView>#include <QGraphicsItem>#include <QDebug> int main(int argc, char** argv) { QApplication app(argc, argv); QGraphicsScene scene; QGraphicsView view; scene.setSceneRect(0, 0, 10, 10); scene.addRect(2, 2, 6, 6, QPen(Qt::black, 0, Qt::DotLine)); QGraphicsItem* item = scene.addRect(3, 3, 5, 5, QPen(Qt::red)); item->setFlag(QGraphicsItem::ItemIsSelectable, 1); qDebug() << item->boundingRect(); qDebug() << item->shape(); view.setScene(&scene); view.resize(900, 900); view.show(); view.scale(80, 80); return app.exec();}
Pythonfrom PySide.QtGui import *from PySide.QtCore import * class TestView(QGraphicsView): def mousePressEvent(self, event): pos = self.mapToScene(event.pos()) print pos items = self.scene().items(pos, Qt.ContainsItemShape, Qt.AscendingOrder) print items #super(TestView, self).mousePressEvent(event) pass
C++ (Qt)/*! Returns all items for the screen position in \a event.*/QList<QGraphicsItem *> QGraphicsScenePrivate::itemsAtPosition(const QPoint &/*screenPos*/, const QPointF &scenePos, QWidget *widget) const{ Q_Q(const QGraphicsScene); QGraphicsView *view = widget ? qobject_cast<QGraphicsView *>(widget->parentWidget()) : 0; if (!view) return q->items(scenePos, Qt::IntersectsItemShape, Qt::DescendingOrder, QTransform()); const QRectF pointRect(scenePos, QSizeF(1, 1)); if (!view->isTransformed()) return q->items(pointRect, Qt::IntersectsItemShape, Qt::DescendingOrder); const QTransform viewTransform = view->viewportTransform(); return q->items(pointRect, Qt::IntersectsItemShape, Qt::DescendingOrder, viewTransform);}
C++ (Qt)const QRectF pointRect(scenePos, QSizeF(1, 1));
C++ (Qt)bool QGraphicsItem::collidesWithPath ( const QPainterPath & path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const [virtual]