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 9.9k 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.
  • 5 Offline
    5 Offline
    52GE
    wrote on 5 Apr 2012, 12:23 last edited by
    #1

    Hi all,

    I am new to Qt and C++.I need to show the contents of a log file using tail command on QTextEdit window.For this i am using QProcess,please let me know how to use QProcess to show tail command output of a log file on QTextEdit Window or is there any other way apart from using QProcess? The .cpp and .h code for the same is given below:-

    LOGFILE.cpp
    @
    logFile::logFile(QWidget *parent):QMainWindow(parent),ui(new Ui::logFile)
    {
    ui->setupUi(this);

    loadTextFile();

    }

    void logFile::on_findButton_clicked()
    {
    QString searchString=ui->lineEdit->text();
    ui->logTextEdit->find(searchString,QTextDocument::FindWholeWords);
    }

    void logFile::loadTextFile()
    {
    QFile file("/var/log/system.log");
    file.open(QIODevice::ReadOnly|QIODevice::Text);

    QTextStream in(&file);
    //QString line=in.readAll();

    QString cmd("tail");
    QProcess* process=new QProcess(this);
    process->start(QFile::encodeName(cmd).data(),QStringList()<<"-f"<</var/log/sys.log");
    process->waitForReadyRead() or waitForFinished;
    QString tmp=process->readAll();
    qdebug()<<tmp;

    file.close();

    ui->logTextEdit->setPlainText(tmp);

    QTextCursor cursor->logTextEdit->textCursor();
    cursor.movePosition(QTextCursor:: Start,QTextCursor::MoveAnchor,1);
    }
    @
    LOGFILE.h
    @
    class logFile:public QMainWindow
    {
    Q_OBJECT
    public:
    logFile(QWidget *parent =0);
    ~logFile();

    public slots:

    void loadTextFile();

    private slots:
    void on_findButton_clicked();
    };
    @

    Here the problem is that i get tailed output but only of the previous command i.e only once the application is closed and reopened ,i can see the output.But i want to see the output then and there.Please help.I don't know where i am going wrong!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 5 Apr 2012, 12:39 last edited by
      #2

      welcome to devnet

      Please use "code wrappers":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 This improves the readability of your code sections. I have introduced them for you this time.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on 5 Apr 2012, 12:58 last edited by
        #3

        The waitForReadyRead is certainly not sufficient. Your program will continue as soon as some information may be read. The waitForFinished may have a similar problem when the command you are executing is taking longer than 30 sec (30000 msec is the default).

        In general it will be better to use with the signals emitted. You should have a look to the "communicating-via-channels":https://qt-project.org/doc/qt-4.8/qprocess.html#communicating-via-channels section of the documentation. This might be a start for you.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • 5 Offline
          5 Offline
          52GE
          wrote on 9 Apr 2012, 05:59 last edited by
          #4

          this is the modification that i did,but now it doesn't give any output on the window!
          Isn't readyReadStandardOutput() an appropriate signal?

          @
          logFile::logFile(QWidget *parent):QMainWindow(parent),ui(new Ui::logFile)
          {
          process=new QProcess();
          connect(process,SIGNAL(readyReadStandardOutput()),this,SLOT(loadTextFile()));
          ui->setupUi(this);
          loadTextFile();
          }

          void logFile::on_findButton_clicked()
          {
          QString searchString=ui->lineEdit->text();
          ui->logTextEdit->find(searchString,QTextDocument::FindWholeWords);
          }

          void logFile::loadTextFile()
          {
          QFile file("/var/log/system.log");
          file.open(QIODevice::ReadOnly|QIODevice::Text);
          QTextStream in(&file);
          //QString line=in.readAll();
          QString cmd("tail");
          QProcess* process=new QProcess(this);
          process->start(QFile::encodeName(cmd).data(),QStringList()<<"-f"<<"/var/log/sys.log");
          //process->waitForReadyRead() or waitForFinished;
          QString tmp=process->readAll();
          qdebug()<<tmp;
          file.close();
          ui->logTextEdit->setPlainText(tmp);
          QTextCursor cursor->logTextEdit->textCursor();
          cursor.movePosition(QTextCursor:: Start,QTextCursor::MoveAnchor,1);
          }
          @

          LOGFILE.H

          @
          class logFile:public QMainWindow
          {
          Q_OBJECT
          public:
          logFile(QWidget *parent =0);
          ~logFile();
          *QProcess process;
          public slots:
          void loadTextFile();
          private slots:
          void on_findButton_clicked();
          };
          @

          Edit: code formatting. Please format your code in a readable manner yourself next time. If you want to get help, it helps if you make it easy on the people that might be able to help you...; Andre

          1 Reply Last reply
          0
          • 5 Offline
            5 Offline
            52GE
            wrote on 9 Apr 2012, 06:14 last edited by
            #5

            I have even tried using,but i can't see any output!
            @
            connect(ui->logTextEdit,SIGNAL(readyReadStandardOutput()),this,SLOT(loadTextFile()));
            @

            logTextEdit is the object name for the QTextEdit

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 9 Apr 2012, 09:29 last edited by
              #6

              There are so many problems in this piece of code, that I don't know where to begin. To name a few:

              You are connecting to the signal of an empty QProcess instance, that is not doing anything

              In your loadTextFile function, you create a new QProcess on each invokation

              That new instance is not the one declared in your class under the same name...

              The new process isn't connected to anything

              You should really create one QProcess instance, connect to that objects signals, and work from there.

              Alternatively, you might look into using QFileSystemWatcher on that file instead of using tail via QProcess.

              1 Reply Last reply
              0
              • 5 Offline
                5 Offline
                52GE
                wrote on 11 Apr 2012, 05:10 last edited by
                #7

                But will QFileSystemWatcher facilitate the usage of tail, i mean i have to show tailed output only,so will the same be possible using QFileSystemWatcher?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on 11 Apr 2012, 06:23 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 11 Apr 2012, 08:10 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 11 Apr 2012, 08:15 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 11 Apr 2012, 12:16 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 11 Apr 2012, 12:19 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 11 Apr 2012, 12:26 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 11 Apr 2012, 21:41 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 6 Feb 2023, 05:15 last edited by
                                #15

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

                                JonBJ 1 Reply Last reply 6 Feb 2023, 16:24
                                0
                                • Aviral 0A Aviral 0
                                  6 Feb 2023, 05:15

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

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on 6 Feb 2023, 16:24 last edited by
                                  #16

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

                                  Aviral 0A 1 Reply Last reply 6 Feb 2023, 17:49
                                  0
                                  • JonBJ JonB
                                    6 Feb 2023, 16:24

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

                                    Aviral 0A Offline
                                    Aviral 0A Offline
                                    Aviral 0
                                    wrote on 6 Feb 2023, 17:49 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