Alphabetical insert policy with QComboBox doesn't work well with my numeric values
-
I have an issue with my editable QComboBox. It contains numeric values (mostly, there's also some text).
For example:
0 (some explanation)
1 (something else)
2
3
..
10
11
12
200Now when the user enters a new value like "26" I naturally want it to appear between 12 and 200. If I use @QComboBox::InsertAlphabetically@ as my insert policy, I get the 26 between the 2 and the 3.
How can I change this behavior? Is there a point where I can intervene with a custom insertion policy?
-
AFAIK, QComboBox doesn't have built-in functionality for sorting numerical strings, so it doesn't have the insertion policy you want.
Perhaps you can implement your own sorting function, then have your program insert the new value at the index you want?
You can also submit a feature request for your insertion policy at http://bugreports.qt-project.org -- I think it's a very reasonable request, seeing as modern operating systems use that policy for sorting filenames.
-
Thanks, JKSH. The insertion happens in the editable combos when user presses Enter, so ideally I'd have to override the insertItem method to put it in my own - only problem with that is that the method is not virtual :)
So I'm guessing I'd need to use eventfilter to actually catch the return and do my own insertion, and that is kind of a lot of code for this silly sort.
-
What you can do is use your own model for the combobox, instead of using the item based approach. Your model may be a QSortFilterProxyModel on top of one of the standard models. Then, all you'd need to do is reimplement the lessThan method of the QSortFilterProxyModel to make sure the sorting is correct.
-
Thanks Andre - that's a good direction. But then, what "insertPolicy" do I set on my combo box to both allow user to insert using "Enter" AND not to force a specific position?
-
Actually, I don't know. It would take a bit of experimentation what happens if you use your own model in combination with the insert policy. My guess is that it would not matter much (unless you use InsertAtCurrent, I'd think), but I did not try this myself.
-
Thanks for the tip anyway! If I get some of that happening, I'll post here.