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 );
}
QAbstractItemModel
can contain images, trees other kinds of data that can be kept in QVariant
. That is why you can't get a QStringList
from it. It is pointless.
However, there is a class QStringListModel
inherited from QAbstractItemModel
that is intended to keep strings. And as you can expect it has method stringList()
.
QComboBox
allows you to change a default model which it uses to another one. By default it uses QStandardItemModel
. Change it to a string list model after creating the combo box.
QStringListModel* cbModel = new QStringListModel();
comboBox->setModel(cbModel);
Now you can get what you want:
QStringList list = cbModel->stringList();