0

I need help in populating a combobox from an array which holds the data that came from a text file, ive already check that the array String had really got the data, the problem is that i cannot populate the combo box from the array.

std::string String[10];
std::string str,tmp;
std::ifstream ifs;
int y=0;
ifs.open("file.txt);
        while (!ifs.eof())
        {
            getline(ifs, str, '\n');
            String[y]=str;
            y++;
        }
        ifs.close();

ui->Destination->addItems(String);

This is what ive got so far....

  • Why do you need an array? You can add strings you read from file directly into the combo box in the `while` loop. – vahancho Jun 13 '17 at 13:36
  • can you somehow given an example on that? – Patrick John Jun 13 '17 at 13:37
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – François Andrieux Jun 13 '17 at 13:39
  • There is nothing wrong with the array, ive already check that using Visual Studio, I just dont know how to populate the combo box with the data inside the array. – Patrick John Jun 13 '17 at 13:44
  • 1
    `QComboBox::addItems` takes a `QStringList` as an argument, so why don't you just use a `QStringList`? Or do as vahancho said and use `QComboBox::addItem` in your `while` loop. – thuga Jun 13 '17 at 13:50
  • That is why im asking a sample on that. But the data came from a text file and i put it into array because i thought i can just do the c++ style of passing things around, but i was wrong. – Patrick John Jun 13 '17 at 13:52
  • 1
    @PatrickJohn, what if you instead of `String[y]=str;` in your example simply write `ui->Destination->addItem(str.c_str());`? Assuming that `ui->Destination` is a combo box. – vahancho Jun 13 '17 at 15:07
  • The problem has already been resolved. – Patrick John Jul 06 '17 at 08:22

0 Answers0