Solved How to display the content which received from other application line by line.
-
Here the below example:
1.Text browser i used to print the string which received form tclsh.exe application
2.I m not able to display the string line by line into text browser, instead at one shot all the string are appended in Text browser widget.eg:
QProcess *myProcess = new QProcess();
arguments << "C:/gui_run.tcl";
myProcess->start("C:/init_config/tclsh.exe",arguments);
myProcess->waitForFinished(3000);
while (myProcess->canReadLine()) {
qDebug("inside while");
rets = myProcess->readLine();
myProcess->waitForFinished(3000);
qDebug("%s", qUtf8Printable(rets));
ui->textBrowser->append(rets);
qDebug("after console print");
}
But qDebug is able to read and display the content line by line but text browser isnt! -
Call https://doc.qt.io/qt-5/qcoreapplication.html#processEvents after each append call.
-
Hi
You are using waitForFinished which is a blocking call so
you are preventing the event loop to run and hence the textBrowser does not
have time to update since you also loop with while.A fix as @AnatolyS says is to use processEvent to force it to run.
This is not considered the best practice and can have side effects but often
it just works as intended if no extra local event loops are used etc.The alternative is to use the signals of QProcess and do it all in
a non blocking asynchronously way. -
@mrjj said in How to display the content which received from other application line by line.:
The alternative is to use the signals of QProcess and do it all in
a non blocking asynchronously way.I'd say that is the only correct solution.
-
@mrjj How you will call waitforfinished in signals? is there any other process call to help out this issue.
-
@Trojan-Arun
Hi
You just hook up to the signals and Qt will call you when data comes in or error happens.void MainWindow::on_pushButton_released() { QProcess *process = new QProcess(this); connect(process, &QProcess::started, []() { qDebug() << "Started!"; }); connect(process, &QProcess::readyRead, [this, process]() { QTextStream outputStream(process->readAllStandardOutput()); ui->textBrowser->append(outputStream.readAll()); }); connect(process, &QProcess::readyReadStandardOutput, [this, process]() { QTextStream outputStream(process->readAllStandardOutput()); ui->textBrowser->append(outputStream.readAll()); }); connect(process, &QProcess::readyReadStandardError, [this, process]() { QTextStream errorStream(process->readAllStandardError()); ui->textBrowser->append(errorStream.readAll()); }); connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), [this, process](int exitCode, QProcess::ExitStatus exitStatus) { ui->textBrowser->append(QString::number(exitCode) + " " + QString::number(exitStatus)); process->deleteLater(); }); process->start("cmd.exe", QStringList() << "/K cd %USERPROFILE%"); }
The sample adds the output to a textbrowser.
You can add your code to readyRead slot and use readline there if needed.