Editable QComboBox: when an item is selected and Return is pressed on it, how to move this item to the top?
-
I'm handling returnPressed() signal of the QComboBox's QLineEdit. If it was an activation of the existing combobox item and not a creation of a new one, I want to move that item to the top of the combobox. What's the easiest way to do that?
-
Hi,
The easiest way I can see is to use your own model and move the entries around as needed
Hope it helps
-
Aww, I have a painful and unproductive history of overriding default models. Anyway, thanks for the advice, hopefully combobox model is simpler than that for a QTreeView :)
-
It is, you can use something like e.g. QAbstractListModel
-
Where can I find the list of methods I must implement for my model to work with QCombobox? Like which of the insert...() methods must I implement and which are not used by QCombobox?
-
Thinking about it again. You don't even need to subclass anything. Just use the currentModel. Add a row at the first place with the new content and remove the old one
-
Turns out it's even simpler, just these 4 lines in a slot subscribed to returnPressed():
@const QString item = itemText(currentIndex());
removeItem(currentIndex());
insertItem(0, item);
setCurrentIndex(0);@P. S. At which point is QLineEdit constructed? It's null in QCombobox constructor, and setLineEdit is not virtual. I wonder if I can find a place to subscribe to QLineEdit from within QCombobox.
-
Also yes, I've over engineered the thing a bit :D