Results 1 to 6 of 6

Thread: running a cmd command using Qt

  1. #1
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default running a cmd command using Qt

    Hello im doing one program that i will run a command in cmd.
    Qt Code:
    1. pro.startDetached("cmd.exe /k ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + QApplication::applicationFilePath());
    To copy to clipboard, switch view to plain text mode 
    But when i do this, show me this error:
    FHVHN.png
    I think the problem is QApplication::applicationFilePath()
    How i could solve it?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: running a cmd command using Qt

    If application file path contains spaces then line 2 should look like this :
    Qt Code:
    1. pro.startDetached("cmd.exe /k ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"" + QApplication::applicationFilePath() + "\"");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: running a cmd command using Qt

    i get the same error

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,525
    Thanked 279 Times in 274 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: running a cmd command using Qt

    OK. Create a full command in QString variable and show it.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,769
    Thanks
    262
    Thanked 773 Times in 763 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: running a cmd command using Qt

    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:

    Qt Code:
    1. QString command( "cmd.exe" );
    2. QStringList params = QStringList() << "/k" << "ping" << "1.1.1.1" << "-n" << ... and so forth
    3.  
    4. QProcess::startDetached( command, params );
    To copy to clipboard, switch view to plain text mode 

    QProcess::startDetached() is a static method, therefore no QProcess instance is required.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    anda_skoa (3rd March 2017)

  7. #6
    Join Date
    Aug 2017
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: running a cmd command using Qt

    Quote Originally Posted by d_stranz View Post
    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:

    Qt Code:
    1. QString command( "cmd.exe" );
    2. QStringList params = QStringList() << "/k" << "ping" << "1.1.1.1" << "-n" << ... and so forth
    3.  
    4. QProcess::startDetached( command, params );
    To copy to clipboard, switch view to plain text mode 

    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
    A little bit of nonsense now and then is cherished by the wisest men...
    - Willy Wonka

Similar Threads

  1. How to Send a command to already running process
    By Rajvesh in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2017, 06:02
  2. read output of command running though ssh
    By Cbr89 in forum Qt Programming
    Replies: 1
    Last Post: 31st January 2017, 08:06
  3. Replies: 2
    Last Post: 7th April 2015, 09:18
  4. Replies: 2
    Last Post: 28th May 2010, 10:16
  5. running shell command from C++ code problem
    By alex chpenst in forum Qt Programming
    Replies: 4
    Last Post: 31st July 2008, 11:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.