diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index 93b49ecf053..eddce7e4072 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -463,6 +463,9 @@ QtWidgets is no longer supported. Construct the item without a scene and then call QGraphicsScene::addItem() to add the item to the scene. +* QAbstractItemView and derived classes only emit the clicked() signal on left click now, + instead of on all mouse clicks. + QtNetwork --------- * QHostAddress::isLoopback() API added. Returns true if the address is diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp index 40aa4292603..929f7db4064 100644 --- a/src/widgets/itemviews/qabstractitemview.cpp +++ b/src/widgets/itemviews/qabstractitemview.cpp @@ -522,7 +522,7 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged() /*! \fn void QAbstractItemView::clicked(const QModelIndex &index) - This signal is emitted when a mouse button is clicked. The item + This signal is emitted when a mouse button is left-clicked. The item the mouse was clicked on is specified by \a index. The signal is only emitted when the index is valid. @@ -1859,7 +1859,8 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event) setState(NoState); if (click) { - emit clicked(index); + if (event->button() == Qt::LeftButton) + emit clicked(index); if (edited) return; QStyleOptionViewItemV4 option = d->viewOptionsV4(); diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index b212a6fdf29..890f30a6687 100644 --- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp +++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp @@ -225,6 +225,7 @@ private slots: void QTBUG6407_extendedSelection(); void QTBUG6753_selectOnSelection(); void testDelegateDestroyEditor(); + void testClickedSignal(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -1508,6 +1509,30 @@ void tst_QAbstractItemView::testDelegateDestroyEditor() QVERIFY(delegate.calledVirtualDtor); } +void tst_QAbstractItemView::testClickedSignal() +{ + QTableWidget view(5, 5); + + view.show(); + QApplication::setActiveWindow(&view); + QVERIFY(QTest::qWaitForWindowActive(&view)); + QCOMPARE(static_cast(&view), QApplication::activeWindow()); + + QModelIndex index49 = view.model()->index(49,0); + QPoint p = view.visualRect(index49).center(); + QVERIFY(view.viewport()->rect().contains(p)); + + QSignalSpy clickedSpy(&view, SIGNAL(clicked(QModelIndex))); + + QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, p); + QCOMPARE(clickedSpy.count(), 1); + + QTest::mouseClick(view.viewport(), Qt::RightButton, 0, p); + // We expect that right-clicks do not cause the clicked() signal to + // be emitted. + QCOMPARE(clickedSpy.count(), 1); + +} QTEST_MAIN(tst_QAbstractItemView) #include "tst_qabstractitemview.moc"