[Qt-interest] QComboBox with a 'no item'?

Andre Somers andre at familiesomers.nl
Sun Jun 6 12:12:29 CEST 2010


Op 6-6-2010 2:06, Timothy Reaves schreef:
> 	I'm using a QComboBox backed by a model.  This forces the user to always have an item selected, once one is selected.   Initially, the index is set to -1, so no item is selected.  Once an item is selected, there is no way to select a blank item.  I could put a little button next to each combo box the clear the selection (setting the index to -1), but this seems a common issue.  Is there a way to do this with the model or the combo box itself?
>
>    

I think it depends a bit on how you want it to look and feel. 
Personally, I am not a big fan of putting a "no selection made" item in 
a database table that otherwise contains valid selections, but that's 
just me. It works, and it is easy, that's for sure.

Alternative approaches, for instance if you can not change your model, 
include:
1) a proxy model that adds the option to the existing list of options 
that you use as the source for your combo box
2) perhaps a clear button inside the line edit of the text? You can find 
examples on how to add a clear button to a line edit in Qt (basically 
just add a QToolButton to a QLineEdit, and using some style sheets on 
the line edit and the tool button to remove the frame from the button 
and add some padding to the richt of the text of the line edit to make 
space for the button). Perhaps you can use the same trick on your combo box?

André




More information about the Qt-interest-old mailing list