Only emit the clicked() signal from views if the left button is used.

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>
This commit is contained in:
Stephen Kelly 2012-08-06 14:30:47 +02:00 committed by Qt by Nokia
parent 2ade633c0a
commit c5d7ea5bad
3 changed files with 31 additions and 2 deletions

3
dist/changes-5.0.0 vendored
View File

@ -463,6 +463,9 @@ QtWidgets
is no longer supported. Construct the item without a scene and then call is no longer supported. Construct the item without a scene and then call
QGraphicsScene::addItem() to add the item to the scene. 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 QtNetwork
--------- ---------
* QHostAddress::isLoopback() API added. Returns true if the address is * QHostAddress::isLoopback() API added. Returns true if the address is

View File

@ -522,7 +522,7 @@ void QAbstractItemViewPrivate::_q_scrollerStateChanged()
/*! /*!
\fn void QAbstractItemView::clicked(const QModelIndex &index) \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 the mouse was clicked on is specified by \a index. The signal is
only emitted when the index is valid. only emitted when the index is valid.
@ -1859,7 +1859,8 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
setState(NoState); setState(NoState);
if (click) { if (click) {
emit clicked(index); if (event->button() == Qt::LeftButton)
emit clicked(index);
if (edited) if (edited)
return; return;
QStyleOptionViewItemV4 option = d->viewOptionsV4(); QStyleOptionViewItemV4 option = d->viewOptionsV4();

View File

@ -225,6 +225,7 @@ private slots:
void QTBUG6407_extendedSelection(); void QTBUG6407_extendedSelection();
void QTBUG6753_selectOnSelection(); void QTBUG6753_selectOnSelection();
void testDelegateDestroyEditor(); void testDelegateDestroyEditor();
void testClickedSignal();
}; };
class MyAbstractItemDelegate : public QAbstractItemDelegate class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -1508,6 +1509,30 @@ void tst_QAbstractItemView::testDelegateDestroyEditor()
QVERIFY(delegate.calledVirtualDtor); 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) QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc" #include "tst_qabstractitemview.moc"