Reputation: 174
Im using this code in Qt to get output of a cmd commend
QProcess c_output;
c_output.start("some-exe", QStringList() << "param1" << "param2" << "param3...");
if (!c_output.waitForStarted())
std::cout << false;
c_output.write("...");
c_output.closeWriteChannel();
if (!c_output.waitForFinished())
std::cout << false;
its work just good.
with this code i can access output with c_output.readAll()
, but problem is this code wait until cmd finish exec ... and then give all output in c_output.readAll()
, i want to get output in realtime and show them in gui of my program
my mean is my commend print multiply lines after exec, i want to show all of them one by one in my program and not wait for it to finish.
Upvotes: 1
Views: 856
Reputation: 1153
You can use waitForReadyRead
instead of the waitForFinished
, see at https://doc.qt.io/qt-5/qprocess.html#waitForReadyRead
Here is a simple example with the usage:
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QProcess c_output;
c_output.setProcessChannelMode(QProcess::MergedChannels);
c_output.start("dmesg", QStringList() << "-w");
if (!c_output.waitForStarted()){
qDebug() << "Failed to start";
return -1;
}
c_output.write("...");
c_output.closeWriteChannel();
while (c_output.state() != QProcess::NotRunning)
{
qDebug() << ".";
if (c_output.waitForReadyRead())
qDebug() << "c_output" << c_output.readAllStandardOutput();
}
return app.exec();
}
Upvotes: 2