T.Poe
T.Poe

Reputation: 2079

QComboBox relation between editTextChanged and currentIndexChanged

Usage of the signal currentIndexChanged is clear, it's triggered when a different item from combobox is selected. But I'm facing a problem with the other signal. editTextChanged is triggered when the editText of combobox is changed manually by the user overwriting it, but also when another item from combobox is selected, so both editTextChanged and currentIndexChanged are triggered.

My problem is that I need to know which one of these two possibilities is happening in my slot on_comboBox_editTextChanged(), whether the text was overwritten or a different item was selected. From what I saw, the slot on_comboBox_editTextChanged() is called the first, so I don't know how to achieve this information in the slot.

I haven't found anything in the QComboBox class which could solve it. So the question is how to differentiate whether editTextChanged was triggered because of index was changed or user overwritten the editText?

Upvotes: 2

Views: 3086

Answers (2)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38528

You can ask the index of the selected item in the editTextChanged handler. If -1 is returned, then the text was edited, else the item was selected from the drop down list. If the text was typed in the text box but it exists in the drop down list, it is the same case as user selected the item from the drop down list.

currentIndex : int

This property holds the index of the current item in the combobox.

The current index can change when inserting or removing items.

By default, for an empty combo box or a combo box in which no current item is set, this property has a value of -1.

Upvotes: 0

Mohammad Kanan
Mohammad Kanan

Reputation: 4582

Editable QComboBox will have an associated default linedit set object, which also can be interfaced directly for signals, like: textEdited , so you may opt to use an alternative to the &QComboBox::editTextChanged signal, with textEdited Signal of the linedit object, for instance:

connect(ui->comboBox->lineEdit(), &QLineEdit::textEdited, this, &MainWindow::textEdited);

So, in this case, when you select an item in the combobox, you will only get the QComboBox signal currentTextChanged, but not the textEdited which is a distinguish for your case.

Upvotes: 3

Related Questions