PDA

View Full Version : Use cmd commands in QT



davinciomare
6th October 2016, 14:30
Hi i was checking and anyone can use commands very similar in cmd like dir mkdir etc.
But for example when i try to use command (cd ..) i couldn't
Code:

QProcess consola;
consola.start("cmd.exe /C " + comando);
consola.waitForFinished();
consola.waitForReadyRead();

anda_skoa
6th October 2016, 16:11
First, when your command needs arguments, like here, use the start() function that takes a list of arguments instead.

Second, you have forgotten to include the error you are getting.

Cheers,
_

davinciomare
6th October 2016, 16:17
the error is when i do
comando= "cd .."
consola.start(comando);
consola.waitForFinished();
consola.waitForReadyRead();

Nothing happen. it's this my problem thx in advance.
I want to move since cmd console. but i am stand in the same path.

One question if for example i pass the next string = "cd C:\"
how i can take next argument to use like a path.

the code that i have

QProcess consola;
#ifdef Q_OS_WIN
if(comando=="cd .."){
directorio.cd("..");
}else{
consola.start("cmd.exe /C " + comando);
}
#else
consola.start(comando);
#endif
consola.waitForFinished();
consola.waitForReadyRead();

Directorio is the folder where i am. obviously i need to put inside of directorio.cd the path but if i write for example this string in comando = "cd C:\" how i can get the path???

at the momento not work. and the directorio.cd(".."); doesnt work too. when i do this show me this message QIODevice::read (QProcess): device not open

anda_skoa
6th October 2016, 16:40
the error is when i do
comando= "cd .."
consola.start(comando);
consola.waitForFinished();
consola.waitForReadyRead();

Nothing happen. it's this my problem thx in advance.

You don't even look at the error.

But anyhow, even if that executes correctly, what what that be good for?
You start cmd.exe and tell it to change its working directory, then it ends?


One question if for example i pass the next string = "cd C:\"
how i can take next argument to use like a path.

Next as in after "C:\"?



Directorio is the folder where i am. obviously i need to put inside of directorio.cd the path but if i write for example this string in comando = "cd C:\" how i can get the path???

If that is the command then it is absolutely useless, right?

You just make the cmd program switch its path and then it ends.

Cheers,
_

d_stranz
6th October 2016, 16:55
You just make the cmd program switch its path and then it ends.

I think davinciomare does not understand that his QProcess creates a new console, which executes his commands and then exits. The commands are not executed in the console where he runs this program, but in a new one that exists only for the time his program is running. The new console has no effect on his current console.

davinciomare
6th October 2016, 17:03
Stranz you are right because when i do dir .. show me the previous folder but for something qprocess créate all time a new console so i coudnt remember.... the old path.........

davinciomare
7th October 2016, 00:45
I think davinciomare does not understand that his QProcess creates a new console, which executes his commands and then exits. The commands are not executed in the console where he runs this program, but in a new one that exists only for the time his program is running. The new console has no effect on his current console.

i define console like global but happen the same.

jefftee
7th October 2016, 01:06
so i dont close the console????? this is the solution? thx in advance
I don't think that's what he said at all. Run "cmd /?" and read the help for the /C option. It runs a single command and exits. Stringing together multiple executions like:


cmd.exe /C cd /some/path
cmd.exe /C cd ..

Runs two different process that operate completely independently. I.e. each process will get its own environment variables, one of which will be the current working directory (typically your user home directory, etc). Changing the current directory with the first command has no bearing on the current working directory of the 2nd, because their each independent processes and start/finish, etc.

What I suspect you want to do, is write a Windows cmd script that you can then execute with QProcess. The cmd script would include all of the steps you wish to execute. You would write/test this script from your Windows command line and once it does exactly what you want, then you can attempt to execute that script via QProcess.

Hope that helps.

davinciomare
7th October 2016, 09:54
i can't to do two at the same time two pointers at the same time can't be added say me qt. i can change path only that qprocess create me all time a new console.. this is the problem..

Lesiok
7th October 2016, 11:11
Tell us what You want to do using QProcess ?

jefftee
7th October 2016, 13:22
i can't to do two at the same time two pointers at the same time can't be added say me qt. i can change path only that qprocess create me all time a new console.. this is the problem..Sorry, I have no idea what you're trying to say. By using QProcess, it will always create a new process (that's what it's used for). If you don't want to create a new process (console), don't use QProcess???

davinciomare
7th October 2016, 20:53
I want to have the same console i can change the path with setWorkingDirectory but i think will not remember the same path.... when i again do one command again ñ.ñ

jefftee
8th October 2016, 00:17
I want to have the same console i can change the path with setWorkingDirectory but i think will not remember the same path.... when i again do one command again ñ.ñ
If you're trying to use QProcess to execute command like "cd", then you clearly don't understand what you're doing. Sorry for being so blunt, but you have to understand what you're trying to do and how to accomplish that goal.

Why don't you list all of the things you expect to be able to do via QProcess? My guess is all of those things should be done in a script and you should be using QProcess to execute the script, not each individual command.