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. How to use QProcess to show tail command output of a log file on QTextEdit Window?
Forum Updated to NodeBB v4.3 + New Features

How to use QProcess to show tail command output of a log file on QTextEdit Window?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 6 Posters 10.1k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #8

    No, QFileSystemWatcher is obviously no tail command. It just notifies you when a file is modified. You can use that to read any new data that was appended since the last time you read the file. Isn't that what tail does too?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #9

      You can mimic part of tail -f by holding a QTimer that for example fires every second. In a slot connected to the timout signal of that timer, you just check the still open file for new data and read that.

      @
      class logFile: QObject (or QWidget) {
      public:
      logFile(QObject *parent);

      protected slots:
      readData();

      private:
      QTimer timer;
      QFile logFile;
      QTextStream logStream;
      };

      logFile::logFile(QObject *parent)
      {
      logFile.setFileName("/var/log/system.log");
      logFile.open(QIODevice::ReadOnly|QIODevice::Text);
      logStream.setDevice(logFile);
      // read the current file contents here if needed

      connect(&timer, SIGNAL(timeout()), this, SLOT(readData()));
      timer.start(1000);
      

      }

      void logFile::readData()
      {
      while(!logStream.atEnd()) {
      QString line = logStream.readLine();
      // supposing logTextEdit is a QPlainTextEdit
      ui->logTextEdit->appendPlainText(line);
      ui->logTextEdit->appendPlainText("\n");
      }
      }
      @

      [Brain to terminal, not tested.]

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #10

        In that case, I think I'd simply use QFileSystemWatcher. No timer needed, just get notified when the file actually changes...

        1 Reply Last reply
        0
        • 5 Offline
          5 Offline
          52GE
          wrote on last edited by
          #11

          as suggested i have changed the code and am using QFileSystemWatcher.But i am still not getting the result,i think there is some mistake somewhere in my code,i can't figure out.

          Ui form appears for a fraction of second and then the application terminates unexpectedly.

          here's the code:-

          LOGFILE.h
          @
          public slots:
          void readContents(QString);
          @

          LOGFILE.cpp

          @
          logFile::logFile(QWidget *parent):QMainWindow(parent),ui(new Ui::logFile)
          {
          QFile *logFile = new QFile("/path/to/log/file");
          new QTextStream(logFile);
          QFileSystemWatcher fileWatcher = new QFileSystemWatcher(this) ;
          fileWatcher->addPath("/path/to/log/file");
          QObject::connect(fileWatcher, SIGNAL(fileChanged(QString)), this, SLOT(readContents(QString))) ;
          }

          void logFile::readContents(QString path)
          {
          QFile *logFile;
          QString textAppended ;
          int newSize = logFile->size();
          int oldSize;
          if(newSize > oldSize)
          {
          if(!logFile->open(QIODevice::ReadOnly))
          {
          textAppended = "Error in opening the File";
          //showOutput();
          ui->logTextEdit->setPlainText(textAppended);
          return;
          }
          else
          {
          logFile->seek((logFile->size() - 1) - (newSize - oldSize));
          QTextStream txtStream(logFile);
          while(!txtStream.atEnd())
          {
          textAppended = txtStream.readLine();
          }
          // showOutput();
          ui->logTextEdit->setPlainText(textAppended);
          }
          oldSize = newSize;
          logFile->close();
          }
          }
          @

          1 Reply Last reply
          0
          • 5 Offline
            5 Offline
            52GE
            wrote on last edited by
            #12

            the file path is same as before i.e /var/log/system.log

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #13

              Please use proper code indentation. I have a hard time reading unindented code, and that's not effort I want to spend. Use the edit link next to the message to adapt it please.

              Edit: thanks for updating, much easier to read.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #14

                In readContents(), logFile is not initialized. Your program crashes.

                In the constructor, you create a QTextStream, but don't keep it anywhere.

                May I ask if you have any experience with C++ at all? It dosen't look so, and I would suggest to take the time for some online tutorial on C++ before you continue with Qt.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • Aviral 0A Offline
                  Aviral 0A Offline
                  Aviral 0
                  wrote on last edited by
                  #15

                  Can you please share the Link of whole code? maybe github or anything?

                  JonBJ 1 Reply Last reply
                  0
                  • Aviral 0A Aviral 0

                    Can you please share the Link of whole code? maybe github or anything?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #16

                    @Aviral-0 From a post over a decade ago?

                    Aviral 0A 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Aviral-0 From a post over a decade ago?

                      Aviral 0A Offline
                      Aviral 0A Offline
                      Aviral 0
                      wrote on last edited by
                      #17

                      @JonB hehe! IThats what exactly I am tryingg to do. If anyone can find github link or repositry it will be very helpful for me!

                      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