QComboBox::addItem() clears any editable text
-
wrote on 25 Jul 2018, 12:57 last edited by JonB
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. -
Hi,
What version of Qt are you using ?
On which platform ? -
wrote on 26 Jul 2018, 00:51 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);
-
wrote on 26 Jul 2018, 06:52 last edited by JonB
@SGaist
Sorry, I'm Qt 5.7 distro for Ubuntu 17.04.@hskoglund
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 afterclear()
, 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 ?
-
Can you provide a minimal compilable example to reproduce the behaviour so we are sure we are all working on the same base ?
wrote on 30 Jul 2018, 06:32 last edited by JonB@SGaist
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
-
@SGaist
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
wrote on 12 Dec 2024, 18:56 last edited byhad a similar problem. addItem with a empty combo box wont work and it cleared the editable text (qt5.15 and mint 21.3). While searching for a solution, I stumbled upon this thread . After some digging I found a solution:
When you block the signals before the clear() and enable them after the addItem() it works.
cmb = QComboBox() cmb.setEditable(true) cmb.setCurrentText("Current text") cmb.blockSignals(true); cmb.clear() cmb.addItem("Item 1") cmb.blockSignals(false);