Run command line from Qt app in linux
-
Dear developer,
I'm new in this forum, and also in the Qt community, this forum and community is great! Said that I will expose my problem, probably a rookie question...
I've created a GUI with Qt, and I want to use a button to launch a script in the linux terminal. Let's say the command ls -l
This is my solution which is not working, I expect an output in the terminal, but I don't get anything
void MainWindow::onExecuteButton_clicked()
{
QProcess process;
process.start("ls", QStringList() << "-l");
process.waitForFinished();
}I also tried to use system("ls -l"); but again I dont get any output. I'm pretty it is because I'm new in Qt, and the solution must be very simple but I cant get it.
Any advice?
Thanks in advance
-
Hi and welcome to devnet,
If you want to get the output of that command, you should read the standard output generated.
You should also always check for errors.
-
Thank you SGaist for your quick reply
what you coment I tried, in fact, I tried
using namespace std;
cout<<"checking the use of ls in qt * \n";
QProcess process;
process.setWorkingDirectory("/home/grullo");
process.start("ls", QStringList() << "-l");
process.waitForFinished();
QString StdOut = process.readAllStandardOutput(); //Reads standard output
cout<<endl<<StdOut.toStdString();But still the output is empty, my main worry is that the first cout is not printed. And in that point the code cannot be failing
Thanks in advance
-
@grullo
Hi
Your code works fine for me if i change the home folder
Try to also include the StdError like in the samplehttps://gist.github.com/technoburst/5369746
and see if it gives an error.
-
Thanks @mrjj
I'm not sure why... but now it is working. I introduced some modifications from the example you posted. At least I had a mistake in the path of the working directory.
The last thing related with this issue. When I read the standard output, the process (problem I run in the shell) must finish. The ls command is very fast, but the script I need to run is not ls, and the script can take some hours. I would like to show in real time the standard output to the users, instead of waiting until the end of the process. Is this possible?
Many thanks for all your help!
I copy here the final code, perhaps someone else have my problem
void MainWindow::on_browseParticles_clicked()
{
using namespace std;
cout<<"checking the use of ls in qt * \n";
QProcess process;
process.setWorkingDirectory("/home/grullo");QString Command; //Contains the command to be executed QStringList args; //Contains arguments of the command Command = "ls"; args<<"-l"<<"/home/vilas"; process.start(Command, args, QIODevice::ReadOnly); //Starts execution of command process.waitForFinished(); QString StdOut = process.readAllStandardOutput(); //Reads standard output cout<<"\n Printing the standard output..........\n"; cout<<endl<<StdOut.toStdString(); cout<<"\n Printing the standard error..........\n"; QString StdError = process.readAllStandardError(); //Reads standard error cout<<endl<<StdError.toStdString();
}
-
@grullo said in Run command line from Qt app in linux:
I would like to show in real time the standard output to the users, instead of waiting until the end of the process. Is this possible?
Yes, absolutely you can, but you must change where you read from the sub-process to use signals & slots. Set up to use
readyReadStandardOutput/Error
signals, attached to your slots which make yourreadAllStandardOutput/Error()
calls. Remove theprocess.waitForFinished()
, instead put a slot onfinished
signal. Allow youron_browseParticles_clicked()
to exit after theprocess.start()
call, so that it returns to the main Qt event loop. The process now runs in the background, sending you output to capture tillfinished
is received.You should also slot onto other signals, like
errorOccurred
. You may also want to usesetProcessChannelMode(QProcess::MergedChannels)
.All the above are in the docs page. Be aware that it is possible that your sub-process's output will not be received by your call program at the moment it is produced. It may be buffered at the sending side, till a newline is output or a certain number of bytes have been produced, and you may not be able to do anything about that (unless you have the source of the sub-process).
-
Thank you very much JoB!
I will try that later, because I need to have a beta version working by the end of the week.
Unfortunately, I have other problem and I need your advise again... (sorry I'm new in Qt).
My previous doubt was how to run a command line in a shell. Now my problem is the "same", but I need to run my own program. What I did: Easy, just substituting the "ls -l" by the command to execute my program. But... but program requires to load some libraries... so what I do is
//Load the libraries (it is a script)
QProcess proc;
QString Command = " . /home/grullo/xmip/xmipp.bashrc;
proc.start(Command);
proc.waitForFinished();//Now I launch my script
Command = "myscript";
proc.start(Command);
proc.waitForFinished();
QString StdOut = proc.readAllStandardOutput(); //Reads standard outputcout<<"\n Printing the standard output..........\n";
cout<<endl<<StdOut.toStdString();
cout<<"\n Printing the standard error..........\n";
QString StdError = proc.readAllStandardError(); //Reads standard error
cout<<endl<<StdError.toStdString();If I lauch my script in command line I get an output in the terminal. But not with Qt program. Also I checked and the script never was executed! Am I doing anything wrong?
Thanks in advance and sorry I don't want to bother other people with my code
-
Where is your "myscript" located ?
-
Thanks SGaist
myscript is a file that makes use of the libraries loaded with xmip.bashrc
In fact, fact my post I tried to run myscript providing the absolute path. I mean
QProcess proc;
QString Command = " /home/grullo/xmip/build/myscript";
proc.start(Command);Then an error in the StdError appeared " error while loading shared libraries" that suggest me that the loaded libraries (by the first start process) are not available in the second start process. I think that the start processes are independent each other. I cannot load libraries for the second one. Currently the alternative I'm thinking is to create a bash script with all I need to do. Then I will run that script with just one start process.
I am correct with the idea of independent process?
Thanks in advacne
-
Yes you are because they are completely unrelated.
-
@grullo said in Run command line from Qt app in linux:
//Load the libraries (it is a script)
More than loading the libraries, it seems to me that you're setting the environment for such libraries to be found/used, setting the path, seeting some environment variables, etc. right?.
So I imagine you can run the first command (.bashrc script) and just grab the system environment just after it via QProcessEnvironment::systemEnvironment().
Then, use that QProcessEnvironment object that you received and used it to set the environment of the second process via QProcess::setProcessEnvironment
-
@Pablo-J-Rogina
It might depend exactly what you mean, but I don't think it can work. You cannot run the.bashrc
as aQProcess
from a Qt program and then useQProcessEnvironment::systemEnvironment()
to access the environment which was created in the sub-shell. ThesystemEnvironment
function returns the environment of the calling process (your Qt program), not that of the calledbash
which interprets the.bashrc
file.@grullo
Let us assume @Pablo-J-Rogina 's hunch is correct that you need the environment variables created inxmipp.bashrc
to be set when running yourmyscript
.Because you presently execute these two files as separate processes, your second command will not inherit anything from executing your first command.
What you may be looking for, try
proc.start("bash", QStringList() << "-c" << ". /home/grullo/xmip/xmipp.bashrc; /home/grullo/xmip/build/myscript");
or
proc.start("bash", QStringList() << "-c" << "source /home/grullo/xmip/xmipp.bashrc; /home/grullo/xmip/build/myscript");
Or, it is possible you could edit
/home/grullo/xmip/build/myscript
to gosource /home/grullo/xmip/xmipp.bashrc
as the first thing it does, and then you may able to executemyscript
alone from your Qt program. -
Thank's for your reply, what you said is exactly what it is. First I set some enviroments variables, to be used by the script.
What I could do is to create the shell script by code , and then call with the start the created script. This worked, but I know that's not a good or a very elegant solution.
Then I saw the @JonB solution, and I tested it. It works properly, and its the right solution (not mine dirty script solution) Thanks!. In fact what you propose I tried it without the bash -c, I think that's critical. Good to know it
Anyway I will spend some time trying to set the enviroment as @Pablo-J-Rogina suggested, just to learn.
Now I will try to read in real time the standardOutput.
Thank you to everyone!
-
@grullo said in Run command line from Qt app in linux:
Anyway I will spend some time trying to set the enviroment as @Pablo-J-Rogina suggested, just to learn.
Save time! My approach won't work
@JonB was exactly right. QProcess::systemEnvironment() returns the environment of the Qt app, i.e. the calling process and no sub-process can change the parent environment. That's why you need to source the initial script to alter the environment, since source is a shell built-in command that doesn't create another process... -
Thank you anyway @Pablo-J-Rogina.
Now I'm able to run scripts, I'm trying to read in real time the standard output. I'm finding some problems, but I will keep fighting them by myself. If for tomorrow I'm not able I will ask you help
Thank you everybody!
-
Dear all,
I continue fighting with my app... and I reached a point in which I'm stuck... Of course is again with the issue of launching a shell script. When I click on a button, the app launches an script. However, the script never finish, and always appears this error:
standard Error:
QProcess: Destroyed while process ("bash") is still running.If I run the script outside of Qt it finishes.
I copy here the code I have, just if it helps
QProcess proc;
proc.setWorkingDirectory("/home/grullo/isoXmipp/");
QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp.bashrc";
QString xmipCmdLine;
createXmippScript(xmipCmdLine);
cmdline = cmdline + "; " + xmipCmdLine;
std::cout << cmdline.toStdString() << std::endl;
proc.start("bash", QStringList() << "-c" <<cmdline);
proc.waitForFinished();QString StdOut = proc.readAllStandardOutput(); //Reads standard output
ui->consoleOutput->setText(StdOut);
cout<<"\n standard output..........\n";
cout<<endl<<StdOut.toStdString();cout<<"\n standard error..........\n";
QString StdError = proc.readAllStandardError(); //Reads standard error
cout<<endl<<StdError.toStdString();Thanks in advance
-
@grullo said in Run command line from Qt app in linux:
QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp.bashrc";
I guess the initial dot (".") is making your command line relative, so that path is not found when set the working directory.
In addition, it'd be useful if you handle signal errorOccurred as it helps knowing about any possible error.
-
@Pablo-J-Rogina
Hi Pablo. It's not what you say. If you read the discussion so far, you will discover that the OP's command-line (passed tobash -c
) is deliberately:. /home/grullo/xmip-bundle/build/xmipp.bashrc
There is a space after the dot. That is a
bash
/sh
command which is a synonym forsource
, and reads the following argument as a file of commands intobash
to be executed. Personally, while.
command may be convenient for typing at the keyboard, I would always usesource
in a program/script for clarity.@grullo
First, please do as @Pablo-J-Rogina asks and put a slot onerrorOccurred
signal. Please also check (and debug out) the return result fromproc.waitForFinished();
. Do yourself a favour, especially if you having a problem and trying to debug, always check documentation for functions having return results/error indicators and use them.The implication of the error message you see is that you have allowed the
QProcess proc;
variable to go out of scope while the process is still running. I don't see how that would arise from the code as shown, but there you are. If it were me, I'd try some other command-lines and see whether you always get this or whether it's only with your particular command.Ah, hang on.
bool QProcess::waitForFinished(int msecs = 30000)
times out by default after 30 seconds if not finished. You don't say anything, but I guess you have to wait 30 seconds and then you get the error? (You would have seen this from printing its return result, as I said above, please show us that.) In that case, your code will read any output and thenproc
will be destroyed. Your sub-process will still be running though, and you will get the message you see. Is that it? Assuming so, you need to decide what you're going to do if the process does not run to conclusion in 30 seconds. That is the most obvious explanation to me of what must be happening? You really ought not be usingwaitForFinished()
, and instead be doing it with signals and slots.... -
@JonB said in Run command line from Qt app in linux:
There is a space after the dot. That is a bash/sh command which is a synonym for source,
you're right. That's the problem to post an answer at midnight...
-
@Pablo-J-Rogina
:) It's also why choosing to writesource
rather than.
would make it clearer for the OP and anyone reading the code :)