1

I make a program that reads a file. I have a combobox that reads lines numbers: 1,6,11,..etc. I want to e.g. read lines 1-5 when line number 1 is choosen in combobox and push button is clicked (or read lines 6-10 when line 6 is choosen, end so on). For now I have this.

int line_counter=1;
if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
    while(!stream.atEnd())
    {
        line = stream.readLine ();
        if(!line.isNull ())
        {
            if((line_counter%5)==1)
                ui->comboBox->addItem (line);
            line_counter++;
        }
     }
 }
 stream.flush ();
 file.close ();

void Servers::on_pushButton_clicked()
{

     if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
    for(int i=line_counter;i<line_counter+5;i++)
    {
        ui->textBrowser->setText(stream.readLine(i));
    }
}
    file.close ();

}

1 Answers1

0

You can avoid reading from file each team you want to update textBrowser if the text is already in the comboBox.

First of all connect the pushButton signal with your method:

connect(ui->pushButton, &QPushButton::clicked, this, &Servers::on_pushButton_clicked);

Then change on_pushButton_clicked like so:

void Servers::on_pushButton_clicked()
{
    if(file.open (QIODevice::ReadOnly | QIODevice::Text))
    {
        int index = ui->comboBox->currentIndex();
        int from = 5 * index;
        int to = from + 5;
        QTextStream stream(&file);
        int lineCount = 0;
        QString text;
        QString line;
        while (stream.readLineInto(&line)) {
            if (from >= lineCount && lineCount < to) {
                text += line;
                text += '\n';
            }
            lineCount++;
        }
        ui->textBrowser->setText(text.toString());
    }
}
Silvano Cerza
  • 954
  • 5
  • 16
  • Thank you. Actually when I put it in my code, nothing happens. And I have one more doubt: becuse I don't want to read only the line that is in ComboBox. I also want to read 4 more lines of a file. – Anastazja Frontczak Jul 18 '19 at 08:03
  • My bad, I didn't realize the ComboBox didn't store all the lines. I've edited the answer, now it should work. – Silvano Cerza Jul 18 '19 at 08:21
  • Now it shows all the lines. when index is 1 it shows all 10 lines and when index is 2 it also shows all 10 lines. Adding a lines to the text was a good idea. But there is a problem, because the lines are next to each other and I would like to show them one under another (like in my file). I tried with setTabPosition but it didn't work. – Anastazja Frontczak Jul 18 '19 at 09:37
  • Right, I forgot to add a newline with `text += '\n';`. Now it should be all right. – Silvano Cerza Jul 18 '19 at 09:53
  • The statement "if (index == lineCount || index < lineCount + 5)" is always true because e.g. when index is 1: so 1==1 || 1<6, in next iteration: 1=/=2 || 1<7, and so on. But I had an idea modifying your code for: int linecount= 5*index-4; (because when e.g. index=2 then the line in my file is 6, and index is 1 then the line in my file is 1). And I think this part: if (... || index < lineCount + 5) should be changed and I have an idea how. I made one more variable outside the loop, that is: int new_counter=linecount+5; and the if looks now:if(index == lineCount || lineCount < new_count) – Anastazja Frontczak Jul 18 '19 at 10:27
  • But now the output is first 5 lines of a file no matter what the index is. – Anastazja Frontczak Jul 18 '19 at 10:32
  • I've changed the answer, check it out. This should work, let me know. – Silvano Cerza Jul 18 '19 at 11:00
  • Thank you, but still the file is reading from the first line no matter what the index is. – Anastazja Frontczak Jul 18 '19 at 11:31
  • You mean that only the first 5 lines are returned or that you want to avoid reading the file each time? The first case seems strange, the code now is correct, there must be something else, instead if you want to avoid reading the file each time you could read it one time and save a list of its lines, so you read form there. – Silvano Cerza Jul 18 '19 at 12:16
  • 1
    Thank you for all your help. You've inspired me for solving the problem. I changed some things a little bit and now it's working. :) – Anastazja Frontczak Jul 19 '19 at 09:47