Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ifstream crashes ui?
QtWS25 Last Chance

ifstream crashes ui?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 6 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • timmie124T Offline
    timmie124T Offline
    timmie124
    wrote on last edited by
    #1

    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);
            }
    
    ? 1 Reply Last reply
    0
    • timmie124T timmie124

      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);
              }
      
      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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);
      }
      
      1 Reply Last reply
      0
      • timmie124T Offline
        timmie124T Offline
        timmie124
        wrote on last edited by
        #3

        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.

        ? 1 Reply Last reply
        0
        • timmie124T timmie124

          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.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • timmie124T Offline
            timmie124T Offline
            timmie124
            wrote on last edited by
            #5

            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

            ? 1 Reply Last reply
            0
            • timmie124T timmie124

              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

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @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.

              timmie124T 1 Reply Last reply
              0
              • ? A Former User

                @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.

                timmie124T Offline
                timmie124T Offline
                timmie124
                wrote on last edited by
                #7

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

                ? 1 Reply Last reply
                0
                • timmie124T timmie124

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

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  @timmie124 :-)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    michelson
                    wrote on last edited by
                    #9

                    Mayby just QApplication::processEvents()?

                    ? 1 Reply Last reply
                    0
                    • M michelson

                      Mayby just QApplication::processEvents()?

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi,

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        kshegunovK 1 Reply Last reply
                        0
                        • Paul ColbyP Offline
                          Paul ColbyP Offline
                          Paul Colby
                          wrote on last edited by
                          #12

                          I'd suggest QFileSystemWatcher too.

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            Hi,

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

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #13

                            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.

                            Read and abide by the Qt Code of Conduct

                            Paul ColbyP 1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              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.

                              Paul ColbyP Offline
                              Paul ColbyP Offline
                              Paul Colby
                              wrote on last edited by
                              #14

                              @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.

                              kshegunovK 1 Reply Last reply
                              0
                              • Paul ColbyP Paul Colby

                                @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.

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by kshegunov
                                #15

                                @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.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved