Get the Contents of a QComboBox

Jonathan Mee picture Jonathan Mee · Sep 18, 2014 · Viewed 11.1k times · Source

I need to get an QStringList or an array containing all QStrings in a QComboBox.

I can't find a QComboBox method that does this, in fact I can't even find a QAbstractItemModel method that does this.

Is this really my only option:

std::vector< QString > list( myQComboBox.count() );

for( auto i = 0; i < list.size(); i++ )
{
    list[i] = myQComboBox.itemText( i );
}

Answer

DowntownDev picture DowntownDev · Dec 12, 2016

Your answer looks fine, but you could also use a QStringList instead of a vector.

QStringList itemsInComboBox; 
for (int index = 0; index < ui->combo_box->count(); index++)
    itemsInComboBox << ui->combo_box->itemText(index);