PDA

View Full Version : Populate a combobox from vector<string>



pkirk25
15th November 2006, 20:21
Hi all,

Just installed Qt and having first go with Designer.

If i have a vector<string> names with entries "Tom", "Tick" and "Harry" and a combo box called combo_names how do I have the words Tom, Dick, Harry and Add Name as the options on the drop down list of the combo box.

If someone has a url I should be reading to understand how to do this, that would be very helpful.

Patrick

jacek
15th November 2006, 20:53
You should read this: http://doc.trolltech.com/4.2/designer-using-a-component.html (especially the part about single inheritance approach).

pkirk25
15th November 2006, 23:31
So my code would look like this:

vector<string> names;
size_t i;

combo_box::combo_box: (QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);

for (i = 0; i 1= names.size(); ++i)
{
ui.name_combo->addItem(names[i]);
}

}

Will STL strings work or is there a conversion to QtString I will need to make?

jacek
15th November 2006, 23:52
So my code would look like this:
Yes.


Will STL strings work or is there a conversion to QtString I will need to make?
You have to convert std::string to QString like this:

ui.name_combo->addItem( QString::fromStdString( names[i] ) );
Make sure that Qt was compiled with STL support (AFAIR it's enabled by default, so just check if your program compiles).