PDA

View Full Version : running a cmd command using Qt



SirJonas
1st March 2017, 02:32
Hello im doing one program that i will run a command in cmd.

QProcess pro;
pro.startDetached("cmd.exe /k ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + QApplication::applicationFilePath());
But when i do this, show me this error:
12367
I think the problem is QApplication::applicationFilePath()
How i could solve it?

Lesiok
1st March 2017, 08:06
If application file path contains spaces then line 2 should look like this :
pro.startDetached("cmd.exe /k ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"" + QApplication::applicationFilePath() + "\"");

SirJonas
1st March 2017, 14:45
i get the same error

Lesiok
1st March 2017, 15:05
OK. Create a full command in QString variable and show it.

d_stranz
1st March 2017, 18:12
How many times does this same question have to be answered in this forum?

If you want to run a command with command line parameters using QProcess, you have to give QProcess::start() or QProcess::startDetached() at least two arguments: a QString containing the command to be executed and a QStringList containing the parameters to the command:



QString command( "cmd.exe" );
QStringList params = QStringList() << "/k" << "ping" << "1.1.1.1" << "-n" << ... and so forth

QProcess::startDetached( command, params );


QProcess::startDetached() is a static method, therefore no QProcess instance is required.

teak421
1st August 2017, 05:56
How many times does this same question have to be answered in this forum?

If you want to run a command with command line parameters using QProcess, you have to give QProcess::start() or QProcess::startDetached() at least two arguments: a QString containing the command to be executed and a QStringList containing the parameters to the command:



QString command( "cmd.exe" );
QStringList params = QStringList() << "/k" << "ping" << "1.1.1.1" << "-n" << ... and so forth

QProcess::startDetached( command, params );


QProcess::startDetached() is a static method, therefore no QProcess instance is required.

Hi. I took your code above and used in my program but the actual CMD window does not get displayed... I can see in task manager that it was started. How do you get it to display? Apologies for such a noob question, this is my first week into this program... Which is awesome!

teak