diff --git a/src/widgets/kernel/qwidget_p.h b/src/widgets/kernel/qwidget_p.h index f8ac13ebb74..0c25b6d8237 100644 --- a/src/widgets/kernel/qwidget_p.h +++ b/src/widgets/kernel/qwidget_p.h @@ -888,9 +888,15 @@ inline void QWidgetPrivate::setSharedPainter(QPainter *painter) inline bool QWidgetPrivate::pointInsideRectAndMask(const QPointF &p) const { Q_Q(const QWidget); - // Use QRectF::contains so that (0, -0.1) isn't in, with p.toPoint() it would be - // The adjusted matches QRect semantics: (160,160) isn't contained in QRect(0, 0, 160, 160) - return QRectF(q->rect().adjusted(0, 0, -1, -1)).contains(p) + + // Use QRectF::contains so that (0, -0.1) isn't in, with p.toPoint() it would be in. + // The -1 on right and bottom matches QRect semantics: + // (160,160) isn't contained in QRect(0, 0, 160, 160) + QRect r = q->rect(); + r.setRight(qMax(-1, r.right() - 1)); + r.setBottom(qMax(-1, r.bottom() - 1)); + + return r.toRectF().contains(p) && (!extra || !extra->hasMask || q->testAttribute(Qt::WA_MouseNoMask) || extra->mask.contains(p.toPoint() /* incorrect for the -0.1 case */)); } diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index c880146a971..7da9457d85d 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -11775,11 +11775,27 @@ void tst_QWidget::childAt() grandChild->setAutoFillBackground(true); grandChild->setGeometry(-20, -20, 220, 220); + QWidget *emptyChild = new QWidget(child); + emptyChild->setPalette(Qt::green); + emptyChild->setAutoFillBackground(true); + emptyChild->setGeometry(0, 159, 160, 0); + QVERIFY(!parent.childAt(19, 19)); QVERIFY(!parent.childAt(180, 180)); QCOMPARE(parent.childAt(20, 20), grandChild); QCOMPARE(parent.childAt(179, 179), grandChild); + QCOMPARE(parent.childAt(120, 179), grandChild); + QCOMPARE(parent.childAt(QPointF(120.0, 178.9)), grandChild); + QVERIFY(!parent.childAt(120, 180)); + QVERIFY(!parent.childAt(QPointF(120, 179.1))); + + emptyChild->setGeometry(100, 0, 0, 160); + + QCOMPARE(parent.childAt(120, 120), grandChild); + QCOMPARE(parent.childAt(QPointF(120.5, 120.0)), grandChild); + QCOMPARE(parent.childAt(QPointF(119.5, 120.0)), grandChild); + grandChild->setAttribute(Qt::WA_TransparentForMouseEvents); QCOMPARE(parent.childAt(20, 20), child); QCOMPARE(parent.childAt(179, 179), child);