Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Contents of a QComboBox

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 );
}
like image 719
Jonathan Mee Avatar asked Sep 18 '14 12:09

Jonathan Mee


People also ask

How do I get text from QComboBox?

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() .

What is a QComboBox?

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.

How do I add items to QComboBox?

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().

What is combo box in pyqt5?

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.


1 Answers

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);
like image 134
DowntownDev Avatar answered Oct 21 '22 07:10

DowntownDev