Results 1 to 4 of 4

Thread: Populate a combobox from vector<string>

  1. #1
    Join Date
    Nov 2006
    Location
    Reading, Berkshire, UK
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Populate a combobox from vector<string>

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Populate a combobox from vector<string>

    You should read this: http://doc.trolltech.com/4.2/designe...component.html (especially the part about single inheritance approach).

  3. #3
    Join Date
    Nov 2006
    Location
    Reading, Berkshire, UK
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Populate a combobox from vector<string>

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Populate a combobox from vector<string>

    Quote Originally Posted by pkirk25 View Post
    So my code would look like this:
    Yes.

    Quote Originally Posted by pkirk25 View Post
    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:
    Qt Code:
    1. ui.name_combo->addItem( QString::fromStdString( names[i] ) );
    To copy to clipboard, switch view to plain text mode 
    Make sure that Qt was compiled with STL support (AFAIR it's enabled by default, so just check if your program compiles).

  5. The following user says thank you to jacek for this useful post:

    pkirk25 (16th November 2006)

Similar Threads

  1. Combobox Signals
    By b1 in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2006, 10:21
  2. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53
  3. table with combobox cells
    By mgurbuz in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2006, 12:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.