Dmon443
asked on
random line function
void readFromFile(QLabel &output)
{
QString message;
int numberOfLines = 10; //file has 10 lines of quotes
int random = QRand() % numberOfLines; //create random number 1-10
//open file and create stream
QFile data("messages.txt");
if (data.open(QIODevice::ReadOnly))
QTextStream out(&data);
//read random line of file
for (int i = 1; i < random; i++) {
}
//read line of file to message and output to QLabel
message = out.readLine();
output.setText(message);
}
I am stuck on a this function I am trying to write in Qt, which is supposed to read a random line from a text file and output it into a QLabel. Can't figure out how I would select a random line, I'm guessing I would need a loop to the random number, but don't know how to read that specific line? Please help? Am I on the right track here?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.