ifstream crashes ui?



  • I've been trying to use qt to watch text log files as lines are added and sadly haven't been able to get the results I've been looking for. So I starting to look for alternatives and found that ifstream works for what I'm trying to do, The fstream code works without issue when its ran in Visual studio without QT but when I go to add this code to QT and run the program the ui freezes and I get no errors. I thought it might of been the string to QString conversion but taking that out doesn't help.

        std::string file = "C:/Users/Tim's Desk/Documents/EVE/logs/Chatlogs/Burrito.app_20160523_175005.txt";
            std::ifstream ifs(file);
    
            std::string line;
            while (1) {
                while (std::getline(ifs, line)) {
                    QString Qline = QString::fromStdString(line);
                    ui->textBrowser->append(Qline + "/n");
                   //cout << line << endl;
                }
                ifs.clear();
                //Sleep(50);
            }
    


  • Maybe it's just a typo?

    ui->textBrowser->append(Qline + "/n");   //   ->  "\n"
    

    Btw, why doesn't the following (copy & paste from docs) not work for you?

    QFile file("in.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
      return;
    
    QTextStream in(&file);
    while (!in.atEnd()) {
      QString line = in.readLine();
      process_line(line);
    }
    


  • the "\n" typo didn't fix anything .

    the problem with with the code from the docs is that it stops reading the file when it hits the end. the file that I'm watching is a text file that is constantly being added. I did try to use QFileSystemWatcher but since the process that is writting the text file always has it open it doesn't let the fileChanged() signal to emit unless you manually open the directory and refresh it. I tried to use QDir::refresh on a timer to try and force the fileChange() signal to emit but with no luck. another thing I had tried was hooking up a timer to clear and reread the file, that works if I just want to read the file but if I want to set up an alert such as if(line = string){ QMessageBox::information(0, "alert", "in system");} I'll just get the same alert every time the function is called.



  • @timmie124 I mean, of course the GUI freezes: you have an infinite loop in the main thread (aka GUI thread).



  • so do I just need to put this code into another thread and it wont lock up the UI?

    https://gyazo.com/3c21a23cfe27f289b30aa35905ff749b QT
    https://gyazo.com/7e5ffdd79d369009075a70af4247a7f2 VS2013



  • @timmie124 said:

    so do I just need to put this code into another thread

    That would be the easiest solution, yes. But you could also use a timer and look for new lines in the file every few milliseconds.



  • @Wieland I'll give it a shot thank you!





  • Mayby just QApplication::processEvents()?



  • @michelson Yeah but then usage on one CPU core will stay at 100%.


  • Lifetime Qt Champion

    Hi,

    To add to @Wieland the QSocketNotifier class might be of interest for your use case.



  • I'd suggest QFileSystemWatcher too.


  • Qt Champions 2016

    I'm with @SGaist, QSocketNotifier is appropriate for such things. QFileSystemWatcher will watch for changes in attributes or directory structure, but I don't believe it actually notifies on content changes, but I might be wrong.



  • @kshegunov said:

    QFileSystemWatcher will watch for changes in attributes or directory structure, but I don't believe it actually notifies on content changes, but I might be wrong.

    QFileSystemWatcher::fileChanged says:

    This signal is emitted when the file at the specified path is modified, renamed or removed from disk.

    It does detect file content changes on Windows (using FindFirstChangeNotification and friends) and on Linux (using inotify). It also has OSX and BSD implementations too, but I haven't looked into how they work.

    QSocketNotifier might be the better option, but I'd certainly consider both. It would be an interesting exercise to put together a minimal example of both options to compare their subtleties :)

    Cheers.


  • Qt Champions 2016

    @Paul-Colby

    It does detect file content changes on Windows (using FindFirstChangeNotification and friends)

    Well, MSDN doesn't mention a notification for the file being written as far as I could see, beside perhaps the size change or the last write time attribute. I don't know how responsive those are, but I suppose QFileSystemWatcher is a fair try.

    It would be an interesting exercise to put together a minimal example of both options to compare their subtleties

    Indeed.


Log in to reply
 

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