diff options
author | Alexis Menard <alexis.menard@nokia.com> | 2010-05-05 04:57:26 (GMT) |
---|---|---|
committer | Alexis Menard <alexis.menard@nokia.com> | 2010-05-05 23:44:28 (GMT) |
commit | 3c4d3a65bbce6b1f9e649412f141ee8890a7b6cd (patch) | |
tree | 801be34e129444e7f7c908b31ef27462637342a2 | |
parent | b7513f4a15ecac1adf54f2abdda6b56c89d6bef4 (diff) | |
download | Qt-3c4d3a65bbce6b1f9e649412f141ee8890a7b6cd.zip Qt-3c4d3a65bbce6b1f9e649412f141ee8890a7b6cd.tar.gz Qt-3c4d3a65bbce6b1f9e649412f141ee8890a7b6cd.tar.bz2 |
QGraphicsWidget was not working properly when ItemSendsPositionChanges is false
The geometry was not properly set because QGraphicsWidget rely on itemChange
to update its own geometry. Now when calling setPos we also ensure that
for a widget the geometry will be up to date. Setting the flag
ItemSendsPositionChanges to false for a given widget will give a small
performance boost.
Reviewed-by:janarve
Reviewed-by:bnilsen
-rw-r--r-- | src/gui/graphicsview/qgraphicsitem.cpp | 2 | ||||
-rw-r--r-- | src/gui/graphicsview/qgraphicswidget.cpp | 8 | ||||
-rw-r--r-- | src/gui/graphicsview/qgraphicswidget_p.cpp | 12 | ||||
-rw-r--r-- | src/gui/graphicsview/qgraphicswidget_p.h | 2 | ||||
-rw-r--r-- | tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp | 29 |
5 files changed, 46 insertions, 7 deletions
diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 42abe59..074e571 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -3578,6 +3578,8 @@ void QGraphicsItem::setPos(const QPointF &pos) // Update and repositition. if (!(d_ptr->flags & ItemSendsGeometryChanges)) { d_ptr->setPosHelper(pos); + if (d_ptr->isWidget) + static_cast<QGraphicsWidget *>(this)->d_func()->setGeometryFromSetPos(); return; } diff --git a/src/gui/graphicsview/qgraphicswidget.cpp b/src/gui/graphicsview/qgraphicswidget.cpp index 8b80bc8..3151e76 100644 --- a/src/gui/graphicsview/qgraphicswidget.cpp +++ b/src/gui/graphicsview/qgraphicswidget.cpp @@ -1044,13 +1044,7 @@ QVariant QGraphicsWidget::itemChange(GraphicsItemChange change, const QVariant & } break; case ItemPositionHasChanged: - if (!d->inSetGeometry) { - d->inSetPos = 1; - // Ensure setGeometry is called (avoid recursion when setPos is - // called from within setGeometry). - setGeometry(QRectF(pos(), size())); - d->inSetPos = 0 ; - } + d->setGeometryFromSetPos(); break; case ItemParentChange: { QGraphicsItem *parent = qVariantValue<QGraphicsItem *>(value); diff --git a/src/gui/graphicsview/qgraphicswidget_p.cpp b/src/gui/graphicsview/qgraphicswidget_p.cpp index 1835c74..daa007f 100644 --- a/src/gui/graphicsview/qgraphicswidget_p.cpp +++ b/src/gui/graphicsview/qgraphicswidget_p.cpp @@ -825,6 +825,18 @@ void QGraphicsWidgetPrivate::setLayout_helper(QGraphicsLayout *l) } } +void QGraphicsWidgetPrivate::setGeometryFromSetPos() +{ + if (inSetGeometry) + return; + Q_Q(QGraphicsWidget); + inSetPos = 1; + // Ensure setGeometry is called (avoid recursion when setPos is + // called from within setGeometry). + q->setGeometry(QRectF(pos, q->size())); + inSetPos = 0 ; +} + QT_END_NAMESPACE #endif //QT_NO_GRAPHICSVIEW diff --git a/src/gui/graphicsview/qgraphicswidget_p.h b/src/gui/graphicsview/qgraphicswidget_p.h index 2c5b3bf..140bc0e 100644 --- a/src/gui/graphicsview/qgraphicswidget_p.h +++ b/src/gui/graphicsview/qgraphicswidget_p.h @@ -130,6 +130,8 @@ public: void windowFrameHoverLeaveEvent(QGraphicsSceneHoverEvent *event); bool hasDecoration() const; + void setGeometryFromSetPos(); + // State inline int attributeToBitIndex(Qt::WidgetAttribute att) const { diff --git a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp index 1930a6f..1a56e6b 100644 --- a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp +++ b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp @@ -166,6 +166,7 @@ private slots: void initialShow(); void initialShow2(); void itemChangeEvents(); + void itemSendGeometryPosChangesDeactivated(); // Task fixes void task236127_bspTreeIndexFails(); @@ -2972,6 +2973,34 @@ void tst_QGraphicsWidget::itemChangeEvents() QTRY_VERIFY(!item->valueDuringEvents.value(QEvent::EnabledChange).toBool()); } +void tst_QGraphicsWidget::itemSendGeometryPosChangesDeactivated() +{ + QGraphicsScene scene; + QGraphicsView view(&scene); + QGraphicsWidget *item = new QGraphicsWidget; + scene.addItem(item); + view.show(); + QTest::qWaitForWindowShown(&view); + item->setGeometry(QRectF(0, 0, 50, 50)); + QTRY_COMPARE(item->geometry(), QRectF(0, 0, 50, 50)); + + item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); + item->setGeometry(QRectF(0, 0, 60, 60)); + QCOMPARE(item->geometry(), QRectF(0, 0, 60, 60)); + QCOMPARE(item->pos(), QPointF(0, 0)); + item->setPos(QPointF(10, 10)); + QCOMPARE(item->pos(), QPointF(10, 10)); + QCOMPARE(item->geometry(), QRectF(10, 10, 60, 60)); + + item->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false); + item->setGeometry(QRectF(0, 0, 60, 60)); + QCOMPARE(item->geometry(), QRectF(0, 0, 60, 60)); + QCOMPARE(item->pos(), QPointF(0, 0)); + item->setPos(QPointF(10, 10)); + QCOMPARE(item->pos(), QPointF(10, 10)); + QCOMPARE(item->geometry(), QRectF(10, 10, 60, 60)); +} + void tst_QGraphicsWidget::QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems() { QGraphicsScene scene; |