Results 1 to 4 of 4

Thread: Constantly Update QFile read

  1. #1
    Join Date
    Nov 2014
    Posts
    12
    Thanks
    4

    Default Constantly 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:

    Qt Code:
    1. Hello World.txt
    2.  
    3. 0
    4. 1
    5. 2
    6. 3
    7. 4
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. while (1)
    2. {
    3. if (logfile.is_open())
    4. {
    5. logfile.seekg(0, logfile.end);
    6. string line;
    7. while (true)
    8. {
    9. while (getline(logfile, line))
    10. {
    11. strncpy_s(send_data, line.c_str(), sizeof(send_data));
    12. send_data[sizeof(send_data)-1] = 0;
    13. ...
    14. }
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    In Qt for now I have something like this

    Qt Code:
    1. void PC::on_pushButtonStream_clicked()
    2. {
    3. QFile file(filename);
    4. if(!file.open(QIODevice::ReadOnly))
    5. QMessageBox::information(0,"info",file.errorString());
    6. if(file.isOpen()){
    7. // while(true) // << CRASH
    8. {
    9. while (!file.atEnd()) {
    10. QByteArray line = file.readLine();
    11. ui->textBrowserLog->setText(line);
    12. }
    13. // }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    I’ve also tried to do a

    Qt Code:
    1. while(file.isOpen())
    To copy to clipboard, switch view to plain text mode 

    but it crashes.

    Thank you in advance.

  2. #2
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constantly Update QFile read

    Hi,
    Are you mean created a program to watch your file and update the Qt Program when that file modified ?
    If yes, I think you can use " QFileSystemWatcher "
    Hope this class will solve your problem.

    Best regards,

    myta

  3. The following user says thank you to myta212 for this useful post:

    digimonkey (4th November 2014)

  4. #3
    Join Date
    Nov 2014
    Posts
    12
    Thanks
    4

    Default Re: Constantly Update QFile read

    Quote Originally Posted by myta212 View Post
    Hi,
    Are you mean created a program to watch your file and update the Qt Program when that file modified ?
    If yes, I think you can use " QFileSystemWatcher "
    Hope this class will solve your problem.

    Best regards,

    myta
    Thank you will check it.

    EDIT
    It seems that QFileSystemWatcher is used to monotorize files on folders (directories). What I want is monotorize if there are changes inside the file, in this case a text file.
    Last edited by digimonkey; 4th November 2014 at 09:00.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,459
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constantly Update QFile read

    From QFileSystemWatcher doc : The fileChanged() signal is emitted when a file has been modified, renamed or removed from disk

  6. The following user says thank you to Lesiok for this useful post:

    digimonkey (4th November 2014)

Similar Threads

  1. Read TXT file with QFile
    By puneet in forum Qt Programming
    Replies: 4
    Last Post: 8th April 2014, 13:34
  2. Replies: 3
    Last Post: 3rd May 2013, 20:27
  3. qfile read from last position
    By NathiyaAmose in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2012, 10:21
  4. QFile::read failing on XP SP3
    By drescherjm in forum Qt Programming
    Replies: 7
    Last Post: 15th April 2009, 21:59
  5. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.