summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry Bastian <thierry.bastian@nokia.com>2010-09-29 12:56:38 +0200
committerJason McDonald <jason.mcdonald@nokia.com>2010-10-04 16:36:04 +1000
commitacc1dc498ea733bab2a1f5ef9983829adbbf5735 (patch)
tree9b3907b7bde9d81dd83bb745f346562da8e0d5fa
parent6c53410d8104064b53beccb1f2014c980735aefb (diff)
Fix double painting when adding an item into a linear layout
the problem was that the item is first painted at its default position, then moved by the layout and finally repainted. We now made sure the item is laid out before the first paint event occurs. Task-number: QTBUG-13865 Reviewed-by: bnilsen (cherry picked from commit 33f525e636ef8fa64a15d3e66c56adaea0075bda)
-rw-r--r--src/gui/graphicsview/qgraphicslinearlayout.cpp8
-rw-r--r--tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp40
2 files changed, 46 insertions, 2 deletions
diff --git a/src/gui/graphicsview/qgraphicslinearlayout.cpp b/src/gui/graphicsview/qgraphicslinearlayout.cpp
index 1588364e97..7e8d19f2aa 100644
--- a/src/gui/graphicsview/qgraphicslinearlayout.cpp
+++ b/src/gui/graphicsview/qgraphicslinearlayout.cpp
@@ -275,13 +275,17 @@ void QGraphicsLinearLayout::insertItem(int index, QGraphicsLayoutItem *item)
qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself");
return;
}
+ Q_ASSERT(item);
+
+ //the order of the following instructions is very important because
+ //invalidating the layout before adding the child item will make the layout happen
+ //before we try to paint the item
+ invalidate();
d->addChildLayoutItem(item);
- Q_ASSERT(item);
d->fixIndex(&index);
d->engine.insertRow(index, d->orientation);
new QGridLayoutItem(&d->engine, item, d->gridRow(index), d->gridColumn(index), 1, 1, 0, index);
- invalidate();
}
/*!
diff --git a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp
index ddc4f7309f..7f24ddc4a9 100644
--- a/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp
+++ b/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp
@@ -184,6 +184,7 @@ private slots:
void task250119_shortcutContext();
void QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems();
void QT_BUG_12056_tabFocusFirstUnsetWhenRemovingItems();
+ void QT_BUG_13865_doublePaintWhenAddingASubItem();
};
@@ -3321,6 +3322,45 @@ void tst_QGraphicsWidget::QT_BUG_12056_tabFocusFirstUnsetWhenRemovingItems()
//This should not crash
}
+
+struct GreenWidget : public QGraphicsWidget
+{
+ GreenWidget() : count(0)
+ {
+ }
+
+ void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * )
+ {
+ count++;
+ painter->setPen(Qt::green);
+ painter->drawRect(option->rect.adjusted(0,0,-1,-1));
+ }
+
+ int count;
+};
+
+void tst_QGraphicsWidget::QT_BUG_13865_doublePaintWhenAddingASubItem()
+{
+ QGraphicsScene scene;
+ QGraphicsView view(&scene);
+ QGraphicsWidget *widget = new QGraphicsWidget;
+ widget->resize(100, 100);
+ scene.addItem(widget);
+ QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(widget);
+
+ view.show();
+ QTest::qWaitForWindowShown(&view);
+
+
+ GreenWidget *sub = new GreenWidget;
+ layout->addItem(sub);
+
+ QTest::qWait(100);
+ QCOMPARE(sub->count, 1); //it should only be painted once
+
+}
+
+
QTEST_MAIN(tst_QGraphicsWidget)
#include "tst_qgraphicswidget.moc"