I need to get an QStringList
or an array containing all QString
s 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 );
}
currentIndex() to get the index of the currently selected item in the combobox, or use . currentText() to get the text. With the index you can also look up the text of a specific item using . itemText() .
A QComboBox provides a means of presenting a list of options to the user in a way that takes up the minimum amount of screen space. A combobox is a selection widget that displays the current item, and can pop up a list of selectable items. A combobox may be editable, allowing the user to modify each item in the list.
A combobox can be populated using the insert functions, insertItem() and insertItems() for example. Items can be changed with setItemText(). An item can be removed with removeItem() and all items can be removed with clear().
A QComboBox object presents a dropdown list of items to select from. It takes minimum screen space on the form required to display only the currently selected item. A Combo box can be set to be editable; it can also store pixmap objects.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With