PDA

View Full Version : QCompler in QComboBox strange behaviour



typedef
4th April 2013, 11:57
Hi,

I instanciate a QComboBox with a completion mode like this


combo->setEditable(true);
combo->setInsertPolicy(QComboBox::NoInsert);
combo->completer()->setCompletionMode(QCompleter::PopupCompletion);


Then I fill the combo with items like this


combo->addItem(QIcon("someIcon"), "Choice A", 42);
combo->addItem(QIcon("someIcon"), "Choice A", 4242);
combo->addItem(QIcon("someIcon"), "Choice B", 43);
combo->addItem(QIcon("someIcon"), "Choice B", 4343);


I have duplicate choice A and B with different QVariant attached to the items.
at runtime when I use the combo with the full dropdown list, my activated() slot give me currentIndex() like this :

I select first Choice A -> currentIndex = 0
I select second Choice A -> currentIndex = 1
I select first Choice B -> currentIndex = 2
I select second Choice B -> currentIndex = 3

the problem is when I do not use the dropdown, but use the completion it give me this :

completion for first Choice A -> currentIndex = 0
completion for second Choice A -> currentIndex = 0
completion for first Choice B -> currentIndex = 2
completion for second Choice B -> currentIndex = 2

My guess is that the Completer do a findItem with the text of the completer, and then return me the first item that is named like that in the combobox, but it seems like a problem.
Does someone know why it does that ? and how can I have the right currentIndex even with completion.
Thank you

Santosh Reddy
4th April 2013, 14:00
Does someone know why it does that ? and how can I have the right currentIndex even with completion.
This is default behavior of the QCompleter. There is some flexibility in selecting the completion role, but that too will not get you what you want. The problem here is that the item's data in DisplayRole is not unique.

I think it is not possible as long as the DisplayRole data is not unique

typedef
4th April 2013, 15:29
ok, so do you think I can achieve what I want if I implement my own QCompleter and set it to the QCombo ?

Added after 53 minutes:

I just had a look at QCombo source and it appears I just can't do this !



int QComboBox::findData(const QVariant &data, int role, Qt::MatchFlags flags) const
{
Q_D(const QComboBox);
QModelIndexList result;
QModelIndex start = d->model->index(0, d->modelColumn, d->root);
result = d->model->match(start, role, data, 1, flags);
if (result.isEmpty())
return -1;
return result.first().row();
}


I f I understand well, the model match have a hits argument setted to 1 hardcoded, so it will always take the first match, and findData return the first result anyway so I just can't do it with ComboBox even with a derivation of QCompleter or QSortFilterProxyModel.