Solved How to change QComboBox highlighted item when a particular signal is emitted
-
Hi,
How do i go about changing the highlighted item of the QComboBox's drop down list whenever a signal is emitted? Mouse event is disabled in my case. So by using setCurrentIndex(index), I could change the item in the ComboBox easily but the drop down list is not reflecting the change. Please advise. Thank you.
-
Get the model index coresponding to the item
i
in the list:const QModelIndex idx = combobox->model()->index(i, combobox->modelColumn(), combobox->rootModelIndex());
Select that index:
combobox->view()->selectionModel()->select(idx, QItemSelectionModel::Select);
(see https://doc.qt.io/qt-5/qitemselectionmodel.html#SelectionFlag-enum to customise how it gets selected) -
You can use
combobox->view()->selectionModel()
to corol selected items in the dropdown -
@VRonin
Hi, Thank you for your reply. Do I use this?option 1:
combobox->view()->selectionModel()->isSelected()->selectedRows(index);option 2:
QModelIndex selectedItem;
selectedItem.sibling(index,0);
combobox->view()->selectionModel()->setCurrentIndex(selectedItem, QItemSelectionModel::Select);i have tried the above and it do not work. The drop down list is not reflecting the change, Did i do it right?
-
@qt-1234
You don't want theisSelected()
in the middle, that just returns abool
. SimilarlyselectedRows()
returns selected rows. There is aselect()
slot to do selection. -
Get the model index coresponding to the item
i
in the list:const QModelIndex idx = combobox->model()->index(i, combobox->modelColumn(), combobox->rootModelIndex());
Select that index:
combobox->view()->selectionModel()->select(idx, QItemSelectionModel::Select);
(see https://doc.qt.io/qt-5/qitemselectionmodel.html#SelectionFlag-enum to customise how it gets selected) -
@VRonin Thank you for your help! This works!