0

this is my first question here, i always found the answers i need but today, that "always" has ended haha. My problem is that i'm trying to read a text file with QFile and QTextStream and save the values inside an STL vector. When i try to read the vector, I obtain that is empty(forgive any mistakes with my english, is no my first language). Here i leave you the methods.

bool TGraphic::process_data( void )
{
bool openingOk = false;
QString line;

if(QFile::exists( this->input_file.fileName() ))
{
    openingOk = this->input_file.open(QIODevice::Text | QIODevice::ReadOnly);
    QTextStream flujo(&input_file);

    while(!(flujo.atEnd()))
    {
        line = flujo.readLine();   
        this->data.push_back( line.toInt() );  // data is std::vector<int> data;
    }
}
return openingOk;
}

The compiler doesn't give me any errors but when i do this

void Ventana::on_pbGraphic_clicked()
{
    imgGen = new TGraphic(fileName);
    std::vector<int> aux(imgGen->getVector());

    bool dataOk, graphicOk;
    img = new QPixmap(400, 300);

    dataOk = imgGen->process_data();

    graphicOk = imgGen->process_graphic(*img);

    if(dataOk && graphicOk && !(aux.empty())) // ** THE LAS CONDITION GAVE ME FALSE **
    {
        //ui->labGraphic->setPixmap(*img);
        ui->labNombreArchivo->setText(QString::number(aux[0])); // I TRIED TO GET THE
    }                                                           // THE FIRST VALUE OF
    else                                                        // THE VECTOR AND THE
    {                                                           // PROGRAM FAILS THERE.
        ui->labGraphic->setText("Error.");
    }
}

in dialog.cpp i get a false. Can you guys help to see where is the problem? If you need extra information ask me. Thanks!

  • `imgGen->process_data()` fills `imgGen->data`. Why do you expect an unrelated variable `aux` to change as a result of this call? It was empty before, and (quite unsurprisingly) it remains empty afterwards. – Igor Tandetnik Dec 02 '14 at 02:39
  • @IgorTandetnik Thank you for answering, but if you pay attetion at line 4: `std::vector aux(imgGen->getVector());` the method is supposedly filling the vector. The aux vector is just a hint to check if the vector was empty. – Emanuel Gómez Arn Dec 02 '14 at 12:20
  • I'm not sure what I'm supposed to see in this line. `aux` becomes a copy of (supposedly empty) `imgGen->getVector()`. Even assuming that `imgGen->getVector()` returns `imgGen->data`, the latter gets filled **after** the copy is made. It's as it you wrote `int x = 0; int y = x; x = 42;` and expected `y` to be non-zero. – Igor Tandetnik Dec 02 '14 at 13:34
  • @IgorTandetnik Ok, i feel so stupid right now :P. My apologies, and thanks! – Emanuel Gómez Arn Dec 02 '14 at 16:06

0 Answers0