How to use QProcess to show tail command output of a log file on QTextEdit Window?
-
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!
-
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.
-
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.
-
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
-
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
-
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.
-
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?
-
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?
-
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 neededconnect(&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.]
-
In that case, I think I'd simply use QFileSystemWatcher. No timer needed, just get notified when the file actually changes...
-
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();
}
}
@ -
the file path is same as before i.e /var/log/system.log
-
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.
-
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.
-
Can you please share the Link of whole code? maybe github or anything?
-
@Aviral-0 From a post over a decade ago?
-
@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!