Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Select an Entry or Add a New One ComboBox

From Qt Wiki
Revision as of 14:20, 24 March 2016 by Wieland (talk | contribs) (clean-up)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The following method acts on a combo box searching for a specified text string and:

  • if the string is present select it
  • if the string is not present add it to the combo box model

In the following piece of code myText is the QString searched for and combo is a reference to the combo box.

bool found = false;
for( int i = 0; i < combo->count() && ! found ; i++ ) {
 if( combo->itemText( i ) == myText ) {
  combo->setCurrentIndex( i );
  found = true;
 }
}
if (!found ) {
 int index = combo->count();
 combo->addItem( myText );
}
combo->setCurrentIndex( index );