Suppress move/resize events if they are the result of call to move()/resize().

QWidget::resize() or QWidget::move() set the new size/position values
and send events. The spontaneous events generated by the platform
should be ignored in that case.

Task-number: QTBUG-30744
Task-number: QTBUG-38768
Task-number: QTBUG-32590
Change-Id: I9c0ae38842ed76a8a88ca64fdc9bbe106b2766b7
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
This commit is contained in:
Friedemann Kleint 2014-05-26 14:28:19 +02:00 committed by The Qt Project
parent c3e416296a
commit a1c5198387
2 changed files with 55 additions and 14 deletions

View File

@ -591,16 +591,24 @@ void QWidgetWindow::updateNormalGeometry()
void QWidgetWindow::handleMoveEvent(QMoveEvent *event) void QWidgetWindow::handleMoveEvent(QMoveEvent *event)
{ {
// If the widget's position already matches that of the event, this is a
// result of call to QWidget::move(), which already sends an event.
const bool spontaneous = m_widget->geometry().topLeft() != event->pos();
updateGeometry(); updateGeometry();
QGuiApplication::sendSpontaneousEvent(m_widget, event); if (spontaneous)
QGuiApplication::sendSpontaneousEvent(m_widget, event);
} }
void QWidgetWindow::handleResizeEvent(QResizeEvent *event) void QWidgetWindow::handleResizeEvent(QResizeEvent *event)
{ {
QSize oldSize = m_widget->data->crect.size(); QSize oldSize = m_widget->data->crect.size();
// If the widget's size already matches that of the event, this is a
// result of call to QWidget::resize(), which already sends an event.
const bool spontaneous = oldSize != event->size();
updateGeometry(); updateGeometry();
QGuiApplication::sendSpontaneousEvent(m_widget, event); if (spontaneous)
QGuiApplication::sendSpontaneousEvent(m_widget, event);
if (m_widget->d_func()->paintOnScreen()) { if (m_widget->d_func()->paintOnScreen()) {
QRegion updateRegion(geometry()); QRegion updateRegion(geometry());

View File

@ -262,6 +262,7 @@ private slots:
void optimizedResizeMove(); void optimizedResizeMove();
void optimizedResize_topLevel(); void optimizedResize_topLevel();
void resizeEvent(); void resizeEvent();
void moveEvent();
void task110173(); void task110173();
void testDeletionInEventHandlers(); void testDeletionInEventHandlers();
@ -2108,41 +2109,53 @@ void tst_QWidget::showFullScreen()
class ResizeWidget : public QWidget { class ResizeWidget : public QWidget {
public: public:
ResizeWidget(QWidget *p = 0) : QWidget(p) ResizeWidget(QWidget *p = 0)
: QWidget(p)
, m_spontaneousResizeEventCount(0)
, m_synthesizedResizeEventCount(0)
, m_spontaneousMoveEventCount(0)
, m_synthesizedMoveEventCount(0)
{ {
setObjectName(QLatin1String("ResizeWidget")); setObjectName(QLatin1String("ResizeWidget"));
setWindowTitle(objectName()); setWindowTitle(objectName());
m_resizeEventCount = 0;
} }
protected: protected:
void resizeEvent(QResizeEvent *e){ void resizeEvent(QResizeEvent *e){
QCOMPARE(size(), e->size()); QCOMPARE(size(), e->size());
++m_resizeEventCount; (e->spontaneous() ? m_spontaneousResizeEventCount : m_synthesizedResizeEventCount)++;
}
void moveEvent(QMoveEvent *e)
{
(e->spontaneous() ? m_spontaneousMoveEventCount : m_synthesizedMoveEventCount)++;
} }
public: public:
int m_resizeEventCount; int m_spontaneousResizeEventCount;
int m_synthesizedResizeEventCount;
int m_spontaneousMoveEventCount;
int m_synthesizedMoveEventCount;
}; };
void tst_QWidget::resizeEvent() void tst_QWidget::resizeEvent()
{ {
QSKIP("QTBUG-30744");
{ {
QWidget wParent; QWidget wParent;
wParent.resize(200, 200); wParent.resize(200, 200);
ResizeWidget wChild(&wParent); ResizeWidget wChild(&wParent);
wParent.show(); wParent.show();
QTest::qWaitForWindowExposed(&wParent); QTest::qWaitForWindowExposed(&wParent);
QCOMPARE (wChild.m_resizeEventCount, 1); // initial resize event before paint QCOMPARE (wChild.m_synthesizedResizeEventCount, 1); // initial resize event before paint
QCOMPARE (wChild.m_spontaneousResizeEventCount, 0);
wParent.hide(); wParent.hide();
QSize safeSize(640,480); QSize safeSize(640,480);
if (wChild.size() == safeSize) if (wChild.size() == safeSize)
safeSize.setWidth(639); safeSize.setWidth(639);
wChild.resize(safeSize); wChild.resize(safeSize);
QCOMPARE (wChild.m_resizeEventCount, 1); QCOMPARE (wChild.m_synthesizedResizeEventCount, 1);
QCOMPARE (wChild.m_spontaneousResizeEventCount, 0);
wParent.show(); wParent.show();
QCOMPARE (wChild.m_resizeEventCount, 2); QCOMPARE (wChild.m_synthesizedResizeEventCount, 2);
QCOMPARE (wChild.m_spontaneousResizeEventCount, 0);
} }
{ {
@ -2150,19 +2163,39 @@ void tst_QWidget::resizeEvent()
wTopLevel.resize(200, 200); wTopLevel.resize(200, 200);
wTopLevel.show(); wTopLevel.show();
QTest::qWaitForWindowExposed(&wTopLevel); QTest::qWaitForWindowExposed(&wTopLevel);
QCOMPARE (wTopLevel.m_resizeEventCount, 1); // initial resize event before paint for toplevels const int synthesizedResizeEventCountAfterShow = wTopLevel.m_synthesizedResizeEventCount;
QCOMPARE (synthesizedResizeEventCountAfterShow, 1); // initial resize event before paint for toplevels
QCOMPARE (wTopLevel.m_spontaneousResizeEventCount, 0);
wTopLevel.hide(); wTopLevel.hide();
QSize safeSize(640,480); QSize safeSize(640,480);
if (wTopLevel.size() == safeSize) if (wTopLevel.size() == safeSize)
safeSize.setWidth(639); safeSize.setWidth(639);
wTopLevel.resize(safeSize); wTopLevel.resize(safeSize);
QCOMPARE (wTopLevel.m_resizeEventCount, 1); QCOMPARE (wTopLevel.m_synthesizedResizeEventCount, synthesizedResizeEventCountAfterShow);
QCOMPARE (wTopLevel.m_spontaneousResizeEventCount, 0);
wTopLevel.show(); wTopLevel.show();
QTest::qWaitForWindowExposed(&wTopLevel); QTest::qWaitForWindowExposed(&wTopLevel);
QCOMPARE (wTopLevel.m_resizeEventCount, 2); #ifdef Q_OS_OSX
QEXPECT_FAIL("", "QTBUG-30744", Abort);
#endif
QCOMPARE (wTopLevel.m_synthesizedResizeEventCount, synthesizedResizeEventCountAfterShow + 1);
QCOMPARE (wTopLevel.m_spontaneousResizeEventCount, 0);
} }
} }
void tst_QWidget::moveEvent()
{
ResizeWidget wTopLevel;
wTopLevel.resize(200, 200);
centerOnScreen(&wTopLevel);
wTopLevel.show();
QTest::qWaitForWindowExposed(&wTopLevel);
const int synthesizedMoveEventCountAfterShow = wTopLevel.m_synthesizedMoveEventCount;
wTopLevel.move(wTopLevel.pos() + QPoint(20, 20));
QTRY_COMPARE (wTopLevel.m_synthesizedMoveEventCount, synthesizedMoveEventCountAfterShow + 1);
QCOMPARE (wTopLevel.m_spontaneousMoveEventCount, 0);
}
void tst_QWidget::showMinimized() void tst_QWidget::showMinimized()
{ {
QWidget plain; QWidget plain;