PDA

View Full Version : Adding items to QComboBox



buster
7th July 2020, 02:25
I have an editable QComboBox *m_combo_name. I modify/correct the text as the user type
QString mod_name = ....
and write the modifed text back, thus:


m_combo_name->lineEdit()->setText(mod_name);

That works fine, but when I try to add more items, from a list, QList<QString> optional_names, thus


for (int i=0; i<optional_names->size(); i++){
m_combo_name->insertItem(i+1, optional_names.at(i));
}

the text in the combobox' LineEdit gets overwritten (I want to preserve the text in the LineEdit). I thought I could avoid that by using the index i+1, but it makes no difference. What am I doing wrong?

Another question: is it possible to open the drop-list, from code, when new items are added? In the same manner as Wikipedia.

Ginsengelf
7th July 2020, 14:27
Hi, insertItem() will append the new item to the list of items, but if you only modify the internal QLineEdit before, there are no items. Therefore the first new item replaces your text.
How do you "modify/correct the text" the user types? Maybe using QValidator/QCompleter may help you.

Ginsengelf

buster
8th July 2020, 05:45
Hi, insertItem() will append the new item to the list of items, but if you only modify the internal QLineEdit before, there are no items. Therefore the first new item replaces your text.
How do you "modify/correct the text" the user types? Maybe using QValidator/QCompleter may help you.

Ginsengelf

That clarified things. I fixed it by inserting the edited text at the head of the list and then inserting all items (strings) to the QComboBox. That's probably the way it is intended to be done.

I still haven't figured out how to open the "drop down" from code, to show the options added. I can open it by calling showPopup(), but then it isn't possible to continue writing. Is there a way to do that?

d_stranz
8th July 2020, 17:40
I can open it by calling showPopup()

showPopup() is probably a nonmodal dialog-type method; that is, the popup widget contains its own event loop to monitor user selections and while it is visible it doesn't respond to changes in the combo box contents. If you want to change this behavior, you may be able to derive from QComboBox and override showPopup() . I don't think it will be very easy to do that and maintain the same behavior that the combobox popup normally has. Its implementation probably depends on there being a fixed number of items in the list, so if you are modifying the list while the user is trying to make a selection, then the indexing has to be carefully managed to avoid returning the wrong index values when a selection is made.

buster
9th July 2020, 21:20
It is strange that QComboBox doesn't implement this behaviour, as it would be very useful. Perhaps it will be included in Qt 6?

d_stranz
10th July 2020, 18:49
It is strange that QComboBox doesn't implement this behaviour

I think the behaviour you propose is strange.

The purpose of the popup is to allow the user to make a selection from the items in the combobox's list, not to display the list of items in a dynamic way. How is the user supposed to make a choice when the list she is seeing is constantly changing underneath her mouse? This would be even worse if the combobox had sorted contents - the contents would keep rearranging themselves, making it impossible to choose the one you want. More often than not, you would probably end up with the wrong one because the list changed underneath you and you didn't notice.

And think how frustrating it would be if each time you opened the popup box, you saw a different list of items. It would be one thing if -you- did something which caused the list to change (like choose a different search term) versus the list changing because the program did it without any input from you.

buster
11th July 2020, 10:18
Maybe I did not explain properly what I meant. What I am after is behaviour somewhat similar to that of Wikipedia, where the list opens up as soon as one starts to type text (the lineedit under the cursor do not change) and then the selection in the drop down list is dynamically altered as one types. What's in the list will of cause have be inserted from code. That saves the user from having to manually open the drop-list.

d_stranz
11th July 2020, 17:05
Ah, then what you want is not a plain QComboBox, but a QComboBox or QLineEdit containing a QCompleter in PopupCompletion mode.