Select an Entry or Add a New One ComboBox: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Decode HTML entity names)
Line 13: Line 13:


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

Revision as of 17:48, 12 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

How to select a specific text entry (or add it if does not exist) in a combo box

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 );