Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QComboBox::addItem() clears any editable text
If you have an editable
QComboBox, the user has typed some text in, the list of items is empty, and code then dynamically adds some new item(s) to the popup list, I find the line edit widget loses the previous text typed in.
# Initialization cmb = QComboBox() cmb.setEditable(True) # Slot for re-populating list dynamically at a later time cmb.clear() # this in itself does not affect/clear the line edit text cmb.addItem(...) # this *does* clear the line edit text
It appears it is the act of inserting the first item into an empty list which loses any edit text.
That in turn seems to be because inserting the first item into an empty list auto-selects that item. When the popup appears I instantaneously see the first item there selected, and its text gets copied to the line edit. I have not seen this documented?
I do not want this behaviour. I clear & re-populate the combobox when the user clicks on it. I need the list to be repopulated, but anything previously typed in should remain as-is.
Is there any way to not have that first
addItem()auto-select and replace the edit text? Otherwise I will have to write code to save the text, re-populate the list, and then re-set the text.
What version of Qt are you using ?
On which platform ?
hskoglund last edited by
Hi, had same problem some time ago, it turns out that only the first addItem() after clear() is lethal i.e. zaps the editable text.
So I resorted to saving/restoring (had only one place in my program were I did clear() on the combobox anyway), something like:
# Slot for re-populating list dynamically at a later time auto savedText = cmb.currentText(); cmb.clear() # this in itself does not affect/clear the line edit text cmb.addItem(...) # this *does* clear the line edit text cmb.setCurrentText(savedText);
Sorry, I'm Qt 5.7 distro for Ubuntu 17.04.
Thank you for confirming you have seen this behaviour too. Yes, as I said I realize I can manually save & restore just like your code. For now I will go do that. But perhaps someone like @SGaist will comment on whether this behaviour ---
addItem()to an empty list, possibly just after
clear(), auto-selects that item and copies its text over anything in the line edit --- is intentional/inevitable or accidental?
Can you provide a minimal compilable example to reproduce the behaviour so we are sure we are all working on the same base ?
The following 5 lines produce the behaviour (Qt 5.7). No signals/slots required. Is this sufficient for a minimal example?
cmb = QComboBox() cmb.setEditable(True) cmb.setCurrentText("Current text") cmb.clear() # this in itself does not affect/clear the line edit text cmb.addItem("Item 1") # this *does* clear the line edit text