When QComboBox looses focus, it auto-selects the matching item



  • Here is the situation:

    Say I have a QComboBox with with two items "Alabama" and "Boston".

    When I type into the QLineEdit portion of the combo box "a", the completion shows up as "labama". When I hit enter on the dialog, "alabama" is the final value of the combobox. If I type "a" and the completion is "labama" and then select a different widget (removing focus from the combobox), the combobox's value becomes "Alabama".

    That's the basic issue. There is more issues for my more specific case...

    "Boston" is different than "Alabama". I have a model for the combobox that uses roles to return special data for Boston than for Alabama. What I want is to be able to type "a" or "A" and have the completion be "labama". But when I type "b" or "B" have no completion.

    To accomplish this I used @QComboBox::completer()->setCompletionRole(CustomCompletionRole)@ and then for the index for Boston @model->setData(index, CustomCompletionRole, QString())@ and in the model handled all unset CustomCompletionRole data as just returning EditRole. So Alabama would complete and Boston wouldn't. That works fine, but if you type out "boston" and then click off of the widget (without just hitting enter to confirm and close the form) it resets to "Boston" as per the first mentioned issue.

    Any thoughts?



  • So are "boston" and "Boston" not the same thing in your combo box's list items? What the combo box is doing is replacing "boston" with an item that matches in it's already known list "Boston". At least that is what it sounds like.

    Why would you not want boston or Boston to autocomplete?

    If you want the user to be able to insert new items i.e. "boston" instead of "Boston" then you can use setInsertPolicy(QComboBox::InsertAtBottom) or something like that.

    I could help more but I'm not really understanding the problem with not wanting to complete certain items and not wanting items that are fully typed to match existing list items. If I understood that I could offer a solution.



  • The exact situation is there is data that is read from a file. Sometimes that data wasn't created correctly and names were entered lower case.

    So there are existing names that are upper case and then a new name that is lower case. I want the user to be able to type "al" and get the existing "Alabama" but if "boston" was imported from the file, I want them to be able to type "Boston" and have it save as "Boston" and not "boston".

    As it is, if "boston" is imported and the user types "Boston" and then hits enter (confirming the form) "Boston" is saved. If the user types "Boston" and then clicks somewhere else on the form, it searches the combo box for something matching and finds "boston" and sets the current index to that.

    Worst case I can create a custom event filter or combo box child class to handle the special case, but I was hoping there was some property like "autoSetOnUnfocus" or something that I was missing.



  • Ok I get it now.

    What I would do is make sure what goes into the combo box is correct. So when you're loading data from the file correct it before adding to the combo.

    If it's as simple as capitalizing the first letter that would be super easy. I can show you the code for that if you'd like. This is the best way to handle it.

    You could also add a signal handler for the combo selection changed signal. Forgot the actual name since I'm not at my computer and don't have the docs. In that handler you can correct the incorrect items. This is a kludgy way though. You're better off making sure the data in the combo was correct in the first place.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.