PDA

View Full Version : How to get lines from a file using editText



lordhomie
2nd December 2019, 23:43
Hello everyone!

here, iam trying to read all the lines from my file into qt textEdit but iam facing problems in calling the ReadAllWords function and setText!

here is my code, i think iam close, because i did it a long time ago but i totally forgot.
Thanks!


void delete1::on_showlines_clicked()
{
Quizlet quizlet; //creating an object of class Quizlet

//quizlet.ReadAllWords(); //calling the function ReadAllWords

ui->textEdit->setText(quizlet.ReadAllWords());
}

well, this is my code for ReadAllWords function to print all the lines from the file:


vector<string> Quizlet::ReadAllWords() {

// File pointer

fstream fin;

fin.open("words.txt", ios::in);

vector <string> rows;

string line, word, temp;

while (getline(fin, line)) {

rows.push_back(line);

stringstream s(line);

}
return rows;

}

and in another source file i want to call the ReafAllFunction into editText,
i hope this helps

Ginsengelf
3rd December 2019, 15:03
Hi,
- QTextEdit::setText() does not accept a vector of std::strings. You need to create a QString from the strings in your vector.
- The line stringstream s(line); does not do anything. The stream is destroyed directly after it was created when the end of the while loop is reached.

Ginsengelf