Only emit the clicked() signal from views if the left button is used.
authorStephen Kelly <stephen.kelly@kdab.com>
Mon, 6 Aug 2012 12:30:47 +0000 (14:30 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 7 Aug 2012 14:40:07 +0000 (16:40 +0200)
This is consistent with QAbstractButton, QCalendarWidget,
QDialogButtonBox and QGroupBox (ie, all other widgets with
a clicked signal)

Task-number: QTBUG-26105

Change-Id: Ieafe988b5c03216796b69a7cd70ac1a03fc12b0a
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
dist/changes-5.0.0
src/widgets/itemviews/qabstractitemview.cpp
tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp

index 93b49ec..eddce7e 100644 (file)
@@ -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
index 40aa429..929f7db 100644 (file)
@@ -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();
index b212a6f..890f30a 100644 (file)
@@ -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<QWidget *>(&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"