diff options
author | J-P Nurmi <jpnurmi@digia.com> | 2013-07-02 15:49:50 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-08 05:58:31 +0200 |
commit | 737abb8a5e51d75c0f2f93d5f7b42b05400034a9 (patch) | |
tree | 28eb60b253fb6b90b4b8793bb8ba1bdb3cf5e45d | |
parent | d0c8fc3b2831de809bdb562db0924673c1abcc84 (diff) |
QComboBox: fix item activation via completer
Task-number: QTBUG-31146
Change-Id: I64291f397d80bf934152f63e629810540abf466e
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
-rw-r--r-- | src/widgets/widgets/qcombobox.cpp | 31 | ||||
-rw-r--r-- | src/widgets/widgets/qcombobox.h | 4 | ||||
-rw-r--r-- | src/widgets/widgets/qcombobox_p.h | 4 | ||||
-rw-r--r-- | tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp | 35 |
4 files changed, 65 insertions, 9 deletions
diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index c59edf295c..afe8f1c3f4 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -59,6 +59,7 @@ #include <qheaderview.h> #include <qmath.h> #include <qmetaobject.h> +#include <qabstractproxymodel.h> #include <private/qguiapplication_p.h> #include <private/qapplication_p.h> #include <private/qcombobox_p.h> @@ -173,18 +174,28 @@ QStyleOptionMenuItem QComboMenuDelegate::getStyleOption(const QStyleOptionViewIt return menuOption; } -#ifdef QT_KEYPAD_NAVIGATION -void QComboBoxPrivate::_q_completerActivated() +#ifndef QT_NO_COMPLETER +void QComboBoxPrivate::_q_completerActivated(const QModelIndex &index) { Q_Q(QComboBox); + if (index.isValid() && q->completer()) { + QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel *>(q->completer()->completionModel()); + if (proxy) { + q->setCurrentIndex(proxy->mapToSource(index).row()); + emitActivated(currentIndex); + } + } + +# ifdef QT_KEYPAD_NAVIGATION if ( QApplication::keypadNavigationEnabled() && q->isEditable() && q->completer() && q->completer()->completionMode() == QCompleter::UnfilteredPopupCompletion ) { q->setEditFocus(false); } +# endif // QT_KEYPAD_NAVIGATION } -#endif +#endif // !QT_NO_COMPLETER void QComboBoxPrivate::updateArrow(QStyle::StateFlag state) { @@ -1149,6 +1160,14 @@ void QComboBoxPrivate::_q_editingFinished() void QComboBoxPrivate::_q_returnPressed() { Q_Q(QComboBox); + + // The insertion code below does not apply when the policy is QComboBox::NoInsert. + // In case a completer is installed, item activation via the completer is handled + // in _q_completerActivated(). Otherwise _q_editingFinished() updates the current + // index as appropriate. + if (insertPolicy == QComboBox::NoInsert) + return; + if (lineEdit && !lineEdit->text().isEmpty()) { if (q->count() >= maxCount && !(this->insertPolicy == QComboBox::InsertAtCurrent)) return; @@ -1191,7 +1210,6 @@ void QComboBoxPrivate::_q_returnPressed() break; } break; - case QComboBox::NoInsert: default: break; } @@ -1393,6 +1411,7 @@ void QComboBox::setAutoCompletion(bool enable) if (d->lineEdit->completer()) return; d->completer = new QCompleter(d->model, d->lineEdit); + connect(d->completer, SIGNAL(activated(QModelIndex)), this, SLOT(_q_completerActivated(QModelIndex))); d->completer->setCaseSensitivity(d->autoCompletionCaseSensitivity); d->completer->setCompletionMode(QCompleter::InlineCompletion); d->completer->setCompletionColumn(d->modelColumn); @@ -1805,8 +1824,10 @@ void QComboBox::setCompleter(QCompleter *c) if (!d->lineEdit) return; d->lineEdit->setCompleter(c); - if (c) + if (c) { + connect(c, SIGNAL(activated(QModelIndex)), this, SLOT(_q_completerActivated(QModelIndex))); c->setWidget(this); + } } /*! diff --git a/src/widgets/widgets/qcombobox.h b/src/widgets/widgets/qcombobox.h index d167ac7d11..2fafe79f7a 100644 --- a/src/widgets/widgets/qcombobox.h +++ b/src/widgets/widgets/qcombobox.h @@ -259,8 +259,8 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end)) Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed()) Q_PRIVATE_SLOT(d_func(), void _q_modelReset()) -#ifdef QT_KEYPAD_NAVIGATION - Q_PRIVATE_SLOT(d_func(), void _q_completerActivated()) +#ifndef QT_NO_COMPLETER + Q_PRIVATE_SLOT(d_func(), void _q_completerActivated(const QModelIndex &index)) #endif }; diff --git a/src/widgets/widgets/qcombobox_p.h b/src/widgets/widgets/qcombobox_p.h index 14cf9e7925..07ba9b0925 100644 --- a/src/widgets/widgets/qcombobox_p.h +++ b/src/widgets/widgets/qcombobox_p.h @@ -348,8 +348,8 @@ public: void _q_emitCurrentIndexChanged(const QModelIndex &index); void _q_modelDestroyed(); void _q_modelReset(); -#ifdef QT_KEYPAD_NAVIGATION - void _q_completerActivated(); +#ifndef QT_NO_COMPLETER + void _q_completerActivated(const QModelIndex &index); #endif void _q_resetButton(); void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index 0bfd4baa0c..82d3fae0fa 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -160,6 +160,7 @@ private slots: void maxVisibleItems(); void task_QTBUG_10491_currentIndexAndModelColumn(); void highlightedSignal(); + void task_QTBUG_31146_popupCompletion(); }; class MyAbstractItemDelegate : public QAbstractItemDelegate @@ -2755,5 +2756,39 @@ void tst_QComboBox::highlightedSignal() QCOMPARE(spy.size(), 1); } +void tst_QComboBox::task_QTBUG_31146_popupCompletion() +{ + QComboBox comboBox; + comboBox.setEditable(true); + comboBox.setAutoCompletion(true); + comboBox.setInsertPolicy(QComboBox::NoInsert); + comboBox.completer()->setCaseSensitivity(Qt::CaseInsensitive); + comboBox.completer()->setCompletionMode(QCompleter::PopupCompletion); + + comboBox.addItems(QStringList() << QStringLiteral("item") << QStringLiteral("item")); + + comboBox.show(); + comboBox.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&comboBox)); + + QCOMPARE(comboBox.currentIndex(), 0); + + comboBox.lineEdit()->selectAll(); + QTest::keyClicks(comboBox.lineEdit(), "item"); + + QTest::keyClick(comboBox.completer()->popup(), Qt::Key_Down); + QTest::keyClick(comboBox.completer()->popup(), Qt::Key_Down); + QTest::keyClick(comboBox.completer()->popup(), Qt::Key_Enter); + QCOMPARE(comboBox.currentIndex(), 1); + + comboBox.lineEdit()->selectAll(); + QTest::keyClicks(comboBox.lineEdit(), "item"); + + QTest::keyClick(comboBox.completer()->popup(), Qt::Key_Up); + QTest::keyClick(comboBox.completer()->popup(), Qt::Key_Up); + QTest::keyClick(comboBox.completer()->popup(), Qt::Key_Enter); + QCOMPARE(comboBox.currentIndex(), 0); +} + QTEST_MAIN(tst_QComboBox) #include "tst_qcombobox.moc" |