Don't emit activated on clicking disabled itemview item.

A itemview item with its flags set to Qt::NoItemFlags is considered
disabled, and therefore should not cause the activated signal
to be emitted.

Task-number: QTBUG-20490
Change-Id: If824505c46f4fcadb9265ad6d1e9337f0cee32cf
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
Mitch Curtis 2013-01-15 11:51:19 +01:00 committed by The Qt Project
parent 9d20f4b629
commit 5c5c9c26d2
2 changed files with 26 additions and 1 deletions

View File

@ -1853,7 +1853,8 @@ void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
QStyleOptionViewItem option = d->viewOptions();
if (d->pressedAlreadySelected)
option.state |= QStyle::State_Selected;
if (style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
if ((model()->flags(index) & Qt::ItemIsEnabled)
&& style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
emit activated(index);
}
}

View File

@ -236,6 +236,7 @@ private slots:
void testClickedSignal();
void testChangeEditorState();
void deselectInSingleSelection();
void testNoActivateOnDisabledItem();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -1642,5 +1643,28 @@ void tst_QAbstractItemView::deselectInSingleSelection()
QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1);
}
void tst_QAbstractItemView::testNoActivateOnDisabledItem()
{
QTreeView treeView;
QStandardItemModel model(1, 1);
QStandardItem *item = new QStandardItem("item");
model.setItem(0, 0, item);
item->setFlags(Qt::NoItemFlags);
treeView.setModel(&model);
treeView.show();
QApplication::setActiveWindow(&treeView);
QVERIFY(QTest::qWaitForWindowActive(&treeView));
QSignalSpy activatedSpy(&treeView, SIGNAL(activated(QModelIndex)));
// Ensure clicking on a disabled item doesn't emit itemActivated.
QModelIndex itemIndex = treeView.model()->index(0, 0);
QPoint clickPos = treeView.visualRect(itemIndex).center();
QTest::mouseClick(treeView.viewport(), Qt::LeftButton, 0, clickPos);
QCOMPARE(activatedSpy.count(), 0);
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"