QMdi: Don't emit subWindowActivated during StyleChange handling.

The handling of StyleChange de-maximizes the child window temporarily,
which was emitting subWindowActivated.
This would crash lokalize, because deactivating a window means deleting
the widgets associated with it, and style-change handling is done in
QApplication by looping over QApplication::allWidgets, which would then
contain dangling pointers.

Full valgrind log at https://bugs.kde.org/show_bug.cgi?id=271494#c7

Change-Id: Ifb24032cde2cd470dcae7cd553ec5ab45a919dd6
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
David Faure 2012-11-28 14:00:45 +01:00 committed by The Qt Project
parent 5c6b2ede3b
commit a6e5ccbe22
2 changed files with 38 additions and 1 deletions

View File

@ -164,6 +164,7 @@
#include <private/qmacstyle_mac_p.h>
#endif
#include <QMdiArea>
#include <QScopedValueRollback>
QT_BEGIN_NAMESPACE
@ -2789,6 +2790,10 @@ bool QMdiSubWindow::event(QEvent *event)
bool wasShaded = isShaded();
bool wasMinimized = isMinimized();
bool wasMaximized = isMaximized();
// Don't emit subWindowActivated, the app doesn't have to know about our hacks
const QScopedValueRollback<bool> activationEnabledSaver(d->activationEnabled);
d->activationEnabled = false;
ensurePolished();
setContentsMargins(0, 0, 0, 0);
if (wasMinimized || wasMaximized || wasShaded)
@ -3008,7 +3013,8 @@ void QMdiSubWindow::changeEvent(QEvent *changeEvent)
if (d->isActive)
d->ensureWindowState(Qt::WindowActive);
emit windowStateChanged(oldState, windowState());
if (d->activationEnabled)
emit windowStateChanged(oldState, windowState());
}
/*!

View File

@ -154,6 +154,7 @@ Q_DECLARE_METATYPE(Qt::WindowState);
Q_DECLARE_METATYPE(Qt::WindowStates);
Q_DECLARE_METATYPE(Qt::WindowType);
Q_DECLARE_METATYPE(Qt::WindowFlags);
Q_DECLARE_METATYPE(QMdiSubWindow*);
class tst_QMdiSubWindow : public QObject
{
@ -204,6 +205,7 @@ private slots:
void task_182852();
void task_233197();
void task_226929();
void styleChange();
};
void tst_QMdiSubWindow::initTestCase()
@ -2018,6 +2020,35 @@ void tst_QMdiSubWindow::task_226929()
QVERIFY(sub1->isMaximized());
}
void tst_QMdiSubWindow::styleChange()
{
QMdiArea mdiArea;
mdiArea.show();
QVERIFY(QTest::qWaitForWindowExposed(&mdiArea));
QMdiSubWindow *sub1 = mdiArea.addSubWindow(new QTextEdit);
sub1->showMaximized();
QMdiSubWindow *sub2 = mdiArea.addSubWindow(new QTextEdit);
sub2->showMinimized();
mdiArea.setActiveSubWindow(sub1);
QTest::qWait(100);
qRegisterMetaType<QMdiSubWindow *>();
QSignalSpy spy(&mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)));
QVERIFY(spy.isValid());
QEvent event(QEvent::StyleChange);
QApplication::sendEvent(sub1, &event);
QApplication::sendEvent(sub2, &event);
// subWindowActivated should NOT be activated by a style change,
// even if internally QMdiSubWindow un-minimizes subwindows temporarily.
QCOMPARE(spy.count(), 0);
}
QTEST_MAIN(tst_QMdiSubWindow)
#include "tst_qmdisubwindow.moc"