From 7e76201808549c0837d1a5154ed8737fb0296c00 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Mon, 18 Jan 2010 10:05:46 +0100 Subject: Fixes: When calling QDesktopWidget::screenGeometry() with a null pointer then it should not crash but warn instead. Task: - RevBy: Andreas AutoTest: Included Details: This also fixes availableGeometry() as well to warn under the same circumstances. --- src/gui/kernel/qdesktopwidget.cpp | 10 ++++++++++ tests/auto/qdesktopwidget/tst_qdesktopwidget.cpp | 25 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/gui/kernel/qdesktopwidget.cpp b/src/gui/kernel/qdesktopwidget.cpp index c8a4373..24b4e57 100644 --- a/src/gui/kernel/qdesktopwidget.cpp +++ b/src/gui/kernel/qdesktopwidget.cpp @@ -47,6 +47,11 @@ QT_BEGIN_NAMESPACE const QRect QDesktopWidget::screenGeometry(const QWidget *widget) const { + if (!widget) { + qWarning("QDesktopWidget::screenGeometry(): Attempt " + "to get the screen geometry of a null widget"); + return QRect(); + } QRect rect = QWidgetPrivate::screenGeometry(widget); if (rect.isNull()) return screenGeometry(screenNumber(widget)); @@ -55,6 +60,11 @@ const QRect QDesktopWidget::screenGeometry(const QWidget *widget) const const QRect QDesktopWidget::availableGeometry(const QWidget *widget) const { + if (!widget) { + qWarning("QDesktopWidget::availableGeometry(): Attempt " + "to get the available geometry of a null widget"); + return QRect(); + } QRect rect = QWidgetPrivate::screenGeometry(widget); if (rect.isNull()) return availableGeometry(screenNumber(widget)); diff --git a/tests/auto/qdesktopwidget/tst_qdesktopwidget.cpp b/tests/auto/qdesktopwidget/tst_qdesktopwidget.cpp index b9d9d7e..d846615 100644 --- a/tests/auto/qdesktopwidget/tst_qdesktopwidget.cpp +++ b/tests/auto/qdesktopwidget/tst_qdesktopwidget.cpp @@ -64,6 +64,7 @@ private slots: void screenNumberForQWidget(); void screenNumberForQPoint(); void availableGeometry(); + void screenGeometry(); }; tst_QDesktopWidget::tst_QDesktopWidget() @@ -98,6 +99,9 @@ void tst_QDesktopWidget::primaryScreen() void tst_QDesktopWidget::availableGeometry() { QDesktopWidget desktop; + QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::availableGeometry(): Attempt " + "to get the available geometry of a null widget"); + desktop.availableGeometry((QWidget *)0); QRect total; QRect available; @@ -158,6 +162,27 @@ void tst_QDesktopWidget::screenNumberForQPoint() QVERIFY(screen >= 0 && screen < desktopWidget->numScreens()); } +void tst_QDesktopWidget::screenGeometry() +{ + QDesktopWidget *desktopWidget = QApplication::desktop(); + QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::screenGeometry(): Attempt " + "to get the screen geometry of a null widget"); + QRect r = desktopWidget->screenGeometry((QWidget *)0); + QVERIFY(r.isNull()); + QWidget widget; + widget.show(); + QTest::qWaitForWindowShown(&widget); + r = desktopWidget->screenGeometry(&widget); + + QRect total; + QRect available; + for (int i = 0; i < desktopWidget->screenCount(); ++i) { + total = desktopWidget->screenGeometry(i); + available = desktopWidget->availableGeometry(i); + } + QVERIFY(total.contains(r)); +} + QTEST_MAIN(tst_QDesktopWidget) #include "tst_qdesktopwidget.moc" -- cgit v0.12