Unsolved start terminal with command by QProcess
-
i wanted to start a specific terminal with a command by QProcess .
i came up this code.QProcess *q1 = new QProcess;; QString k("htop"); q1->setWorkingDirectory(QDir::homePath()); q1->startDetached(k); q1->waitForFinished(); qDebug()<< q1->readAllStandardOutput();
another
QProcess *q1 = new QProcess;; QString k("htop"); QString l(QDir::homePath()); QStringList args; args << k; q1->startDetached("xfce4-terminal",args,l); q1->waitForStarted(); qDebug()<< args<< l;
but nothing opening .
-
Before trying with QProcess you type the command on the command prompt/terminal & check whether it starts the new terminal. After that you can try with QProcess.
-
@dheerendra
it works on the terminal. but it dose not open new terminal .it runs there. -
QProcess executes one single process. What you are trying to do is executing a shell command, not a process. The piping of commands is a feature of your shell.
Put the command you want to be executed as an argument to sh after -c ("command"):
QProcess q1; q1.start("sh", QStringList() << "-c" << "htop");
And this varies between the OS's. On what OS are you, I asume linux, as thats what htop is usually run on.
-
@J.Hilk said in start terminal with command by QProcess:
QProcess q1;
q1.start("sh", QStringList() << "-c" << "htop");i am in linux.
and your way do nothing .
i want a specific terminal app will run the command.NT: htop is a terminal app .
-
@saber
In your sample code you usestartDetached()
, and then try to call all sorts of things on theQProcess
, likereadAllStandardOutput()
. Please read http://doc.qt.io/qt-5/qprocess.html#startDetached to realise you cannot do these things on a detached process. You also cannot run interactive terminals and expect to be able to collect their output. You are also making no effort to check return results from calls or see if there is any error output message from the OS....Begin by using
start()
to run thexterm
terminal command. We know that works and opens a window; we don't know anything about the "terminals" you are trying to run. When you have got that working you can examine the situation with your desired terminal. -
@JonB i just want to run a command on specific terminal.don't want ant output or result to work.
there are some terminal apps in linux that i want to run from my app in separate window.that's it.in short . open a specific terminal in separate window and run a command.
-
@saber said in start terminal with command by QProcess:
i just want to run a command on specific terminal.don't want ant output or result to work.
Your code has:
qDebug()<< q1->readAllStandardOutput();
It's your code, not mine!
i just want to run a command on specific terminal
Your code does not attempt to run any command in the terminal you specify. It just attempts to run the terminal, i.e. presumably interactively. And then you say "it doesn't work" when you're not trying to get it to do whatever it is that you actually want. If you want to run a command in the terminal, why don't you say so, and attempt to do so? We can help you, but not if we have to guess stuff....
-
@JonB sorry . i am not an expert in c++ nor qt.i am doing the job that my co-devloper should do .he is off-line and i can't sit back.
those code are copy past from many question /answers.
qt-forum is the help i can get now .
if you have anything that will work , it will be helpful.
-
@saber
This has nothing to do with C++. I am happy to help you but you will not say what it is you want to do. If you write earlieri want a specific terminal app will run the command.
why won't you say what the command is you want to run?
- Do you want to run an interactive terminal, and the user sits inside it, typing stuff, till he exits?
- Do you want the terminal to start, run some other command, and then exit?
- Or what?
Did you try what I suggested with
xterm
to see that that does open a terminal window, which I think you're saying your attempts do not? -
@JonB thanks.
i want the first one.
and i am trying the xterm way. -
@JonB i tried this
QProcess *Process = new QProcess(); QString exec = "xterm"; QStringList params; params <<"ls"; Process->start(exec,params);
-
@saber
That will not work.Please, you are simply answering inconsistently to each question I ask. I asked whether you want to run an interactive terminal the user sits in and types or whether you wish it to execute a command, to which you replied "i want the first one". But the example you give you are trying (incorrectly) to pass
ls
to it. Which is the second one.If & when you come back and clearly state what you actually wish to do I will answer. But while you say one thing and then try to do something quite different nobody can help you....
-
@JonB ok.
a pushbutton will open the terminal with the htop commnad(as it is the terminal app).
then a separate window will open which contains the terminal with running the htop app.
if user input command(ctrl +c) or the window close button clicked the window will close.(that's why i mean user will interact with the terminal by keyboard).in the command there will be other type of terminal app.
-
@saber
You still don't state what it is you want to happen in the terminal, I'm tearing my hair out! Does it sit there interactively doing nothing waiting for the user to type some commands, or does it run some command (which you won't tell us about), show its output, leave the window there awaitng you to only close it, and that's it?Using
xterm
I'll show you both of these:- Sits interactively:
QProcess *process = new QProcess(); QString exec = "xterm"; process->start(exec);
- Runs
ls -l
, output in thexterm
, does not accept any further commands, leaves window open, user can close window:
QProcess *process = new QProcess(); QString exec = "xterm"; QStringList params; params << "-hold" << "-e" << "ls" << "-l"; process->start(exec, params);
When one of these does whatever you want we might be getting somewhere....?
-
@JonB i want the second one .
just open the terminal with the commnad and sit there .
here is the code for htopQString l(QDir::homePath()); QProcess *process = new QProcess(); QString exec = "xterm"; QStringList params; params << "-hold" << "-e" << "htop" ; process->setWorkingDirectory(l); process->start(exec, params);
output
ther is now two things to do .
- extiting the command with the ctrl +c leave a empty window ,nothing in it.
- the command is running only in the xterm . i tried the xfce-terminal.i want all the known terminal should do this.
thanks
- extiting the command with the ctrl +c leave a empty window ,nothing in it.
-
@saber
If you do not want thexterm
to remain open for closure, remove the-hold
argument.If you want to use something other than
xterm
to run yourhtop
(or whatever), the whole point is that I had toman xterm
to discover that the way it runs a command for you is to specify-e <program> <argument> <argument>
. So if you wantxfce-terminal
instead ofxterm
, you will need to read its manual page to discover what command-line it takes. There will not be a generic "i want all the known terminal should do this", it will vary from terminal to terminal.BTW, if I had wanted to ask your question, now that I know what it is, I would have said something like:
"I want to run a program named
htop
fromQProcess
. However, the problem is that it needs a terminal window to display its output. How do I do that? And btw (for some reason) I'd like the terminal window to be provided byxfce-terminal
." -
@JonB in future i will take much time to clear the qustion.
now i thought it will be easy to open htop in other terminal and that will be universal.
because the htop app has a icon and when it clicked it opes the app in the defult terminal.
-
in future i will take much time to clear the qustion.
Thank you :)
because the htop app has a icon and when it clicked it opes the app in the defult terminal.
You misunderstand how "launching an app from an icon" works. This does not only apply to
htop
."Launching from an icon" under Linux is a feature of your Desktop Manager. And you could be using a variety of those under Linux, e.g. Unity, GNOME, I guess you're using XFCE. Almost certainly you have a
htop.desktop
file somewhere, probably hidden somewhere inside your user area (e.g. try runningfind ~ -name \*.desktop -print
; or it might be in either of/usr/share/applications
or~/.local/share/applications
). When the desktop launches from an icon it reads that file and that tells it what to do. And if you look in that file (it's text format) you''ll see something stating its a "console" or "terminal" application (e.g.Terminal=true
; also see how there is aExec=...
to specify what exactly it is to execute). So when your desktop launches it the desktop creates the console/terminal window for it to run in automatically. When we do it from within another application, like a Qt one, we need to do that ourselves explicitly, hence what you've been trying.