diff options
author | J-P Nurmi <jpnurmi@gmail.com> | 2009-07-01 08:38:51 (GMT) |
---|---|---|
committer | Bjørn Erik Nilsen <bjorn.nilsen@nokia.com> | 2009-07-01 08:38:51 (GMT) |
commit | 728430d16fd62de001ba8eaccabae41feef790f2 (patch) | |
tree | c1a4594a8fa0a81bcdee447449f5cc0dfd9a5c9f | |
parent | 9cb231d773db6deb8fb145eb40aa949a2758d002 (diff) | |
download | Qt-728430d16fd62de001ba8eaccabae41feef790f2.zip Qt-728430d16fd62de001ba8eaccabae41feef790f2.tar.gz Qt-728430d16fd62de001ba8eaccabae41feef790f2.tar.bz2 |
Added QGraphicsScene::sendEvent().
Merge-request: 787
Reviewed-by: Bjørn Erik Nilsen <bjorn.nilsen@nokia.com>
-rw-r--r-- | src/gui/graphicsview/qgraphicsscene.cpp | 28 | ||||
-rw-r--r-- | src/gui/graphicsview/qgraphicsscene.h | 2 | ||||
-rw-r--r-- | tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp | 29 |
3 files changed, 58 insertions, 1 deletions
diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index 0dc77cc..d790640 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -5988,6 +5988,34 @@ void QGraphicsScene::setActiveWindow(QGraphicsWidget *widget) } } +/*! + \since 4.6 + + Sends event \a event to item \a item through possible event filters. + + The event is sent only if the item is enabled. + + Returns \c false if the event was filtered or if the item is disabled. + Otherwise returns the value that was returned from the event handler. + + \sa QGraphicsItem::sceneEvent(), QGraphicsItem::sceneEventFilter() +*/ +bool QGraphicsScene::sendEvent(QGraphicsItem *item, QEvent *event) +{ + Q_D(QGraphicsScene); + if (!item) { + qWarning("QGraphicsScene::sendEvent: cannot send event to a null item"); + return false; + } + if (item->scene() != this) { + qWarning("QGraphicsScene::sendEvent: item %p's scene (%p)" + " is different from this scene (%p)", + item, item->scene(), this); + return false; + } + return d->sendEvent(item, event); +} + void QGraphicsScenePrivate::addView(QGraphicsView *view) { views << view; diff --git a/src/gui/graphicsview/qgraphicsscene.h b/src/gui/graphicsview/qgraphicsscene.h index 4d65b91..c8b147f 100644 --- a/src/gui/graphicsview/qgraphicsscene.h +++ b/src/gui/graphicsview/qgraphicsscene.h @@ -229,6 +229,8 @@ public: QGraphicsWidget *activeWindow() const; void setActiveWindow(QGraphicsWidget *widget); + bool sendEvent(QGraphicsItem *item, QEvent *event); + public Q_SLOTS: void update(const QRectF &rect = QRectF()); void invalidate(const QRectF &rect = QRectF(), SceneLayers layers = AllLayers); diff --git a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp index 6439125..4247cca 100644 --- a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp @@ -141,7 +141,7 @@ protected: } }; -class EventSpy : public QObject +class EventSpy : public QGraphicsWidget { Q_OBJECT public: @@ -151,6 +151,13 @@ public: watched->installEventFilter(this); } + EventSpy(QGraphicsScene *scene, QGraphicsItem *watched, QEvent::Type type) + : _count(0), spied(type) + { + scene->addItem(this); + watched->installSceneEventFilter(this); + } + int count() const { return _count; } protected: @@ -162,6 +169,14 @@ protected: return false; } + bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) + { + Q_UNUSED(watched); + if (event->type() == spied) + ++_count; + return false; + } + int _count; QEvent::Type spied; }; @@ -236,6 +251,7 @@ private slots: void changedSignal(); void stickyFocus_data(); void stickyFocus(); + void sendEvent(); // task specific tests below me void task139710_bspTreeCrash(); @@ -3587,5 +3603,16 @@ void tst_QGraphicsScene::stickyFocus() QCOMPARE(text->hasFocus(), sticky); } +void tst_QGraphicsScene::sendEvent() +{ + QGraphicsScene scene; + QGraphicsTextItem *item = scene.addText(QString()); + EventSpy *spy = new EventSpy(&scene, item, QEvent::User); + QCOMPARE(spy->count(), 0); + QEvent event(QEvent::User); + scene.sendEvent(item, &event); + QCOMPARE(spy->count(), 1); +} + QTEST_MAIN(tst_QGraphicsScene) #include "tst_qgraphicsscene.moc" |