I use QProcess to run another program. The command is airodump-ng mon0.
Its output is changing anytime, and is whole different (also not like ping command, changed by line). I want use QProcess to run that program and get its output while it is still running.
Here is the code:
pushButton_clicked()
{
myprocess = new QProcess(this);
connect(myprocess,SIGNAL(readReadStandardOutput()),this,SLOT(outlog()));
myprocess->start("sudo -A ./airodump-ng mon0");
}
outlog()
{
QString abc = myprocess->readAllStandardOutput();
textEdit->setText(abc);
}
In the program airodump-ng, I've already added code setvbuf(stdout,(char*)NULL,_IONBF,0); to use row buffering.
Related
I want to write a routine that will automatically change my Machine learning parameters and execute the (Machine learning) code multiple times and save its result into a text file.
The Machine learning code is run from the Ubuntu terminal and its result also are written to the Ubuntu terminal.
I need a way to read data and write data to the Ubuntu terminal from the C++ code.
What I have found so far is the QProcess:
QProcess process;
process.start("gnome-terminal"); //THE TERMINAL IS OPENED INDEED
char w[] = "write";
process.write(w,sizeof(w));
process.waitForFinished();
But nothing is written or read from it
What does working is:
QProcess process1;
QProcess process2;
process1.setStandardOutputProcess(&process2);
process1.start("echo 0534230313");
process2.start("sudo -S poweroff");
process2.setProcessChannelMode(QProcess::ForwardedChannels);
And also this works:
int result = system("echo 0534230313 | sudo -S poweroff"); ///COMMANDS TO TERMINAL
int result2 = system("gnome-terminal"); ///OPEN TERMINAL
But the problem is that I wont be able to read the output result (My Machine learning program can open and write to the terminal by itself)
Use the finished signal to read the process output results
connect( poProcess, static_cast<void (QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished),
[=](int,QProcess::ExitStatus)
{
QByteArray oProcessOutput=
poProcess->readAllStandardOutput();
// Parse the process output.
//
// Mark process for deletion.
emit poProcess->deleteLater();
});
I want to run a qprocess (the program adb) and when the process is finished return the results to the calling function. However, there's every possibility that adb could find itself in a loop, printing error messages such as "ADB server didn't ACK" to stdout, not ever finishing. I need to trap these errors.
QProcess run_command;
connect(&run_command,SIGNAL(readyReadStandardOutput()),this,SLOT( dolog() ));
QString result=RunProcess("adb connect 192.168.1.100");
...
QString MainWindow::RunProcess(QString cstring)
{
run_command.start(cstring);
// keep gui active for lengthy processes.
while(run_command.state() != QProcess::NotRunning)
qApp->processEvents();
QString command=run_command.readAll();
return command; // returns nothing if slot is enabled.
}
void MainWindow::dolog()
{
QString logstring = run_command.readAllStandardOutput();
if (logstring.contains("error condition")
logfile("Logfile:"+logstring);
}
If I enable the signal/slot, dolog() prints stdout to a logfile, but RunProcess returns a blank string. If I disable the signal/slot, RunProcess() returns the qprocess output as expected.
First you need to identify which output stream the command in question is using for its errors.
It is very like stderr so you would need to connect to the readyReadStandardError() signal instead.
For the command itself I would recommend to split it into command and arguments and use the QProcess::start() overload that takes the command and a list of arguments.
Just more robust than to rely on a single string being correctly separated again.
I need to run an external exe through Qt application which requires commands to be entered in windows command prompt.
QString exePath = "C:\Windows\system32\cmd.exe";
QProcess pro;
pro.start(exePath);
pro.execute("cmd.exe");
But I got output like below plain cmd prompt
But I want windows command prompt like expected cmd
You need to read from QProcess standart output and print it on screen.
You can use pro.waitForReadyRead() and if it returns true do
QByteArray arr = pro.readAllStandardOutput();
QString str(arr);
qDebug() << str;
Better decision is to use signal slot mechanism and implement onReadyToRead() slot and connect QProcess readyReadStandardOutput() signal to it.
pro.start(exePath);
pro.execute("cmd.exe");
You should not use this two methods at same time, QProcess::execute is static member.
You need to start process detached:
QString exePath = "C:\Windows\system32\cmd.exe";
QProcess pro;
pro.startDetached(exePath);
pro.waitForStarted();
//Event Loop here
I start rsync in QProcess. My process runs fine (in its own terminal) if I use QProcess::startDetached() but nothing happens if I start it with QProcess:start(). The problem seems to be that QProcess can't apparently read messages from rsync and write it to the output window.
I have connect this signal in constructor.
MainWindow::~MainWindow()
{
process = new QProcess(this);
connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(onReadyReadStandardOutput() ) );
}
Later on button clicked I call:
void MainWindow::onButton1Clicked()
{
process->start("rsync -a root#10.0.0.1:/path/ /rsync_folder");
//process->start("ping 10.0.0.01"); // this works for testing and I see output but not the above.
}
When rsync starts, it prints a message and ask for password..none of it is received by my QProcess but the ping message are received..what could be possibly wrong here?
The above grieving line also works directly on windows 7 command line but it just doesn't seem to show any progress in QProcess.
Update
Here is how I displaying the output.
void MainWindow::onReadyReadStandardOutput()
{
qDebug() << process->readAllStandardOutput();
}
http://doc.qt.io/qt-5/qprocess.html#communicating-via-channels
Did you remember to link to and check the standard error channel?
http://doc.qt.io/qt-5/qprocess.html#readAllStandardError
That has fixed it for me in the past for some QProcesses I have started.
Another way I've done it, is to use QProcess::startDetached();
Hope that helps.
My research shows that rsync probably behaves like scp which accordingly this answer doesn't generate output when it is redirected.
How can I start a Shell Script using QProcess?
The Shell Script has eight different commands in it, some with arguments others without.
I tried to start the Shell Script with (using Ubuntu 11.10):
QProcess *Prozess = new QProcess();
Prozess->setWorkingDirectory(MainDirectory);
Prozess->start("/bin/sh", QStringList() << "Shell.sh");
But this doesn't work, that means nothing happens. How to make it work?
Code is fine. Problem is at run-time.
Either your program can't run /bin/sh for some reason (test if you can run gedit instead?), or the MainDirectory variable has wrong directory path (debug it), or the Shell.sh does not exist in that directory (capitalization mistakes? What about "./Shell.sh"?), or you don't have enough privileges to run or modify target directory/files (are they owned by you?).
The process you have started is running in background. if you want to see any explicit output from the running script you have to connect to void readyReadStandardOutput() or/and void readyReadStandardError() and read from the process explicitly. For example:
void onReadyRead() {
QByteArray processOutput = Prozess->readAllStandardOutput();
}
This should work:
QProcess::ProcessError Error = myProcess->readAllStandardError();
return Error;
QProcess ().execute ("/bin/sh " + MainDirectory + "/Shell.sh");
will do the job.