diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index e8a462d3f0c..9525c09ea9c 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -2167,6 +2167,12 @@ void QComboBoxPrivate::setCurrentIndex(const QModelIndex &mi) indexBeforeChange = -1; if (indexChanged || modelResetToEmpty) { + QItemSelectionModel::SelectionFlags selectionMode = QItemSelectionModel::ClearAndSelect; + if (q->view()->selectionBehavior() == QAbstractItemView::SelectRows) + selectionMode.setFlag(QItemSelectionModel::Rows); + if (auto *model = q->view()->selectionModel()) + model->setCurrentIndex(currentIndex, selectionMode); + q->update(); emitCurrentIndexChanged(currentIndex); } @@ -2600,11 +2606,6 @@ void QComboBox::showPopup() return; #endif // Q_OS_MAC - // set current item and select it - QItemSelectionModel::SelectionFlags selectionMode = QItemSelectionModel::ClearAndSelect; - if (view()->selectionBehavior() == QAbstractItemView::SelectRows) - selectionMode.setFlag(QItemSelectionModel::Rows); - view()->selectionModel()->setCurrentIndex(d->currentIndex, selectionMode); QComboBoxPrivateContainer* container = d->viewContainer(); QRect listRect(style->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxListBoxPopup, this)); diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp index 5d8fff15ebb..455ccf3c354 100644 --- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp @@ -3711,6 +3711,8 @@ void tst_QAccessibility::comboBoxTest() QCOMPARE(iface->text(QAccessible::Name), QLatin1String("one")); #endif QCOMPARE(iface->text(QAccessible::Value), QLatin1String("one")); + QCOMPARE(combo.view()->selectionModel()->currentIndex().row(), 0); + combo.setCurrentIndex(2); #ifdef Q_OS_UNIX QCOMPARE(iface->text(QAccessible::Name), QLatin1String("three")); @@ -3721,7 +3723,13 @@ void tst_QAccessibility::comboBoxTest() QCOMPARE(listIface->role(), QAccessible::List); QCOMPARE(listIface->childCount(), 3); + QAccessibleSelectionInterface *selectionIface = listIface->selectionInterface(); + QVERIFY(selectionIface); + QCOMPARE(selectionIface->selectedItemCount(), 1); + QCOMPARE(listIface->indexOfChild(selectionIface->selectedItem(0)), 2); + QVERIFY(!combo.view()->isVisible()); + QCOMPARE(combo.view()->selectionModel()->currentIndex().row(), 2); QVERIFY(iface->actionInterface()); QCOMPARE(iface->actionInterface()->actionNames(), QStringList() << QAccessibleActionInterface::showMenuAction() << QAccessibleActionInterface::pressAction()); iface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());