Constant update QFile Read



  • Hi,
    Im pretty new at Qt and english isnt my main language but I will try to explain my doubt.

    Im creating a program that reads a text file and print its last line in a textBox after you press a button (Stream). The thing is I want the textBox to update automatically after a new line is add in the text file, while I keep read the text file and dont close it.

    For example, this is my text file at first time I read it:
    @
    Hello World.txt

    0
    1
    2
    3
    4
    @

    There should be printed '4' in the textBox. (until here all OK)

    But after the first read I add a new line in the text file with the number '5', then the program should update automatically the textBox and add that new line.

    I've made the same program a few months ago in C++ but now I want to put it in Qt

    @
    while (1)
    {
    if (logfile.is_open())
    {
    logfile.seekg(0, logfile.end);
    string line;
    while (true)
    {
    while (getline(logfile, line))
    {
    strncpy_s(send_data, line.c_str(), sizeof(send_data));
    send_data[sizeof(send_data)-1] = 0;
    ...
    }
    }
    }
    }
    @

    In Qt for now I have something like this
    @
    void PC::on_pushButtonStream_clicked()
    {
    QFile file(filename);

    if(!file.open(QIODevice::ReadOnly))
        QMessageBox::information(0,"info",file.errorString());
    
    if(file.isOpen()){
    

    // while(true) // << CRASH
    {
    while (!file.atEnd()) {
    QByteArray line = file.readLine();
    ui->textBrowserLog->setText(line);
    }
    // }
    }
    }@

    For now I have to click all time the Stream button to update the textBox

    I've also tried to do a
    @while(file.isOpen())@
    but it crashes.

    Thank you in advance.



  • Hi..
    why are you writing same instructions as c++ code .
    while(true) is not necessary to write .



  • bq. Hi..
    why are you writing same instructions as c++ code .
    while(true) is not necessary to write .

    Yeah I took that away, but it wont work the way I want it to work anyway.
    Now Im trying to use QFileSystemWatcher to monotorize if there are any change to the text file and to trigger the update in the textBox.

    But still reading how it works.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.