Hello everyone here is my concern, it concerns c with Qt:
Part of my project is to go display a random line from a txt file containing names, I made a first program that does that and it works perfectly, here it is (here the random number is defined).
std::ifstream file("noms.txt");
std::string line;
std::string response_string = "452";
int randomLine = std::stoi(response_string);
for (int i = 0; i <= randomLine; i ) {
std::getline(file, line);}
std::cout << line;
but when i implement this code when my program complete (here is the relevant part)
std::ifstream file("noms.txt");
std::string line;
int randomLine = std::stoi(response_string);
for (int i = 0; i <= randomLine; i ) {
std::getline(file, line);}
QString qstringLine = QString::fromStdString(line);
nom->setText(qstringLine);
Here, when I put some value in the label "name", it is displayed, but when I put the line of the txt, it does not work (Here, the response_string variable is a character (but a number) coming from an api)
Have-you any idea ?
I have already tried not to convert the line to QString, I have already tried to display only the result of the api (it works), I think the problem is between reading the line and the display. I also tried to update the widget.
CodePudding user response:
QTextStream
class and QTextStream::readLine()
function can do it better.
Example:
QFile data("noms.txt");
const auto line_num{200};
QString line;
if (data.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&data);
for (auto i{0}; i < line_num && stream.readLineInto(&line); i) {}
}
qDebug() << "line num 200:" << line;
CodePudding user response:
I close this post, thank you for your help but I found (a problem of path and access permissions).