[Solved] QProcess
-
wrote on 29 Dec 2013, 17:03 last edited by
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] -
wrote on 29 Dec 2013, 18:17 last edited by
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?
-
wrote on 31 Dec 2013, 02:43 last edited by
No it doesn't. In fact it doesn't seem to run the program ls at all, at least there is no Output.txt located on the file system. And it does not enter the readfromProcess() function.
-
wrote on 31 Dec 2013, 02:46 last edited by
I have tried a lot of different approaches, but to get any output at all I had to
[code]
process.start("bash");
process.write("bash commands");
[/code] -
wrote on 1 Jan 2014, 00:01 last edited by
Well, I did get ls to run, and it did finally link to the output. One thing is ls is located on my file system from /bin. The arguments don't seem to work with the QStringList though.
-
wrote on 2 Jan 2014, 16:59 last edited by
Can you post your solution code?
-
wrote on 2 Jan 2014, 18:09 last edited by
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().
-
wrote on 3 Jan 2014, 01:33 last edited by
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.
-
wrote on 3 Jan 2014, 01:44 last edited by
I may just try concatenating a complex command and running bash. It will be messy and it may not work, but neither has the last 50 attempts.
-
wrote on 3 Jan 2014, 16:53 last edited by
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();
@ -
wrote on 4 Jan 2014, 00:21 last edited by
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]
1/11