diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index 6ecf5a664f..319cc86c18 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -1703,6 +1703,7 @@ bool QAbstractItemView::viewportEvent(QEvent *event) d->viewportEnteredNeeded = true; break; case QEvent::Leave: + d->setHoverIndex(QModelIndex()); // If we've left, no hover should be needed anymore #ifndef QT_NO_STATUSTIP if (d->shouldClearStatusTip && d->parent) { QString empty; diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index d241296a6b..f2cf78e8ac 100644 --- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp +++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -151,6 +152,7 @@ private slots: void testSelectionModelInSyncWithView(); void testClickToSelect(); void testDialogAsEditor(); + void QTBUG46785_mouseout_hover_state(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -2213,5 +2215,56 @@ void tst_QAbstractItemView::testDialogAsEditor() QCOMPARE(delegate.result, QDialog::Accepted); } +class HoverItemDelegate : public QItemDelegate +{ +public: + HoverItemDelegate() + : QItemDelegate() + , m_paintedWithoutHover(false) + { } + + void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const override + { + Q_UNUSED(painter); + + if (!(opt.state & QStyle::State_MouseOver)) { + + // We don't want to set m_paintedWithoutHover for any item so check for the item at 0,0 + if (index.row() == 0 && index.column() == 0) { + m_paintedWithoutHover = true; + } + } + } + + mutable bool m_paintedWithoutHover; +}; + +void tst_QAbstractItemView::QTBUG46785_mouseout_hover_state() +{ + HoverItemDelegate delegate; + + QTableWidget table(5, 5); + table.verticalHeader()->hide(); + table.horizontalHeader()->hide(); + table.setMouseTracking(true); + table.setItemDelegate(&delegate); + centerOnScreen(&table); + table.show(); + QVERIFY(QTest::qWaitForWindowActive(&table)); + + QModelIndex item = table.model()->index(0, 0); + QRect itemRect = table.visualRect(item); + + // Move the mouse into the center of the item at 0,0 to cause a paint event to occur + QTest::mouseMove(table.viewport(), itemRect.center()); + QTest::mouseClick(table.viewport(), Qt::LeftButton, 0, itemRect.center()); + + delegate.m_paintedWithoutHover = false; + + QTest::mouseMove(table.viewport(), QPoint(-50, 0)); + + QTRY_VERIFY(delegate.m_paintedWithoutHover); +} + QTEST_MAIN(tst_QAbstractItemView) #include "tst_qabstractitemview.moc"