[Solved] QProcess
-
Solution: Know your filesystem
I have read the documentation, and tried different approaches based on other examples. I am trying to run some bash commands and return the results as a string.
[code]
process = new QProcess();
QObject::connect(process, SIGNAL(readyReadStandardOutput()), SLOT(readfromProcess()));
process->start("bash");
process->write("ls > output.txt");readfromProcess()
{
while(process->canReadLine())
qDebug() << QString(process->readLine());
}
[/code]First problem is, the Signal and Slot connection isn't working. Second problem is the file doesn't get created until after the program closes. And third problem is, the output or the read() functions don't seem to work at least if they are called in the same function as the creation and start of the process. Also, I cannot seem to get the arguments to work when I create a StringList.
[code]
QStringList list;
list << "ls > Output.txt";
process.start("bash", list);
[/code] -
I would do the following:
don't call bash as the program you want to execute but directly "ls" with absolute path and arguments ">" & "output"
use the QProcess::waitForFinished() function to start the file reading
Here the code with the two modifications:
@QProcess process;
process.start("/usr/bin/ls", QStringList() << ">" << "Output.txt");
if(process.waitForFinished())
readfromProcess();@Does this solves your problem?
-
I have just tested the next code and it does the job correctly:
@
process.setStandardOutputFile("output.txt");
process.start("/bin/ls -lA");
process.waitForFinished();
@The command "ls < output.txt" doesn't work (I don't know why), that is why I call the function setStandardOutputFile().
-
Well, I don't actually have a working code for what I am trying to do yet. The problem with the < output.txt is the piping. < is a pipe and that is a separate process, so you need to create a second process to handle the piping... or more neatly as you did it. Once the ls program terminates, so does the thread or the process. That is also why starting bash and writing a pipe into it did work, because the process is running bash and doesn't terminate. So it can then it seems handle a piping within that thread or process. (at least that is what I have gathered, and filled in with my own imagination) I still have not gotten QStringList to work and I will most likely need it.
-
But, sorry I do not understand. The code I have posted above doesn't solve your problem? What do you need exactly?
By the way, this code with QStringList args also works:
@
process.setStandardOutputFile("output.txt");
process.start("/bin/ls", QStringList() << "-lA");
process.waitForFinished();
@ -
I see. I was looking at the arguments in the wrong framework I think. I am trying to add more commands or process'. I am trying to interface with namecoind daemon. So querying the namecoin database, and receiving Json objects. I do have the process working correctly now, thank you.
[code]
process.start("/usr/local/bin/namecoind", QStringList() << "name_show" << inputString);
[/code]