Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. CMD commands execution from the qt application
Forum Updated to NodeBB v4.3 + New Features

CMD commands execution from the qt application

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 881 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pranit Patil
    wrote on 17 Aug 2018, 15:27 last edited by
    #1

    I am building qt application to run CMD commands from the qt application itself.
    Here is my code:
    im using Qt creator 4.6.0 Os-Windows 7 - 32bit
    #include "command.h"
    #include "ui_command.h"
    #include <QProcess>
    #include <QDir>
    #include<QMessageBox>

    Command::Command(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Command)
    {
    ui->setupUi(this);
    process = new(QProcess);

    connect(ui->send, SIGNAL(clicked()),this, SLOT(execute_process()));
    connect(ui->cmdline, SIGNAL(textChanged(const QString&)),this, SLOT(enable_send(const QString&)));
    

    }

    Command::~Command()
    {
    delete ui;
    }

    void Command::execute_process()
    {
    ui->textBrowser->setPlainText("");

    QString command = ui->cmdline->text().trimmed();
    QStringList cmd_list = command.split(" ");
    QString program = cmd_list[0];
    QStringList arguments;
    for (int i = 1; i < cmd_list.size(); ++i) {
        arguments.append(cmd_list[i]);
        fill_outputText(cmd_list[i]);
        fill_outputText("\n");
    }
    
    process->start(program, arguments);
    process->waitForFinished();
    fill_outputText(process->readAllStandardOutput());
    fill_outputText(process->readAllStandardError());
    

    }
    void Command::fill_outputText(const QString& text)
    {
    ui->textBrowser->setPlainText(ui->textBrowser->toPlainText() + text);
    }

    I am executing following command from the qt ui :

    cmd /c "G:\Install Software\Arduino\hardware\tools\avr/bin/avrdude" -C"G:\Install Software\Arduino\hardware\tools\avr/etc/avrdude.conf"

    and getting following response :
    /c
    "G:\Install
    Software\Arduino\hardware\tools\avr/bin/avrdude"
    -C"G:\Install
    Software\Arduino\hardware\tools\avr/etc/avrdude.conf"
    -v
    -patmega2560
    -cwiring
    -PCOM14
    -b115200
    -D
    -Uflash:w:"C:\Users\USER\AppData\Local\Temp\arduino_build_139930/Blink.ino.hex":i

    Where as I am expecting following:
    /c
    "G:\Install
    Software\Arduino\hardware\tools\avr/bin/avrdude"
    -C"G:\Install
    Software\Arduino\hardware\tools\avr/etc/avrdude.conf"
    -v
    -patmega2560
    -cwiring
    -PCOM14
    -b115200
    -D
    -Uflash:w:"C:\Users\USER\AppData\Local\Temp\arduino_build_139930/Blink.ino.hex":

    where as I am expecting something like this ( what ever I am getting in CMD ):
    0_1534519057651_Screenshot_19.png

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Aug 2018, 17:34 last edited by
      #2

      @Pranit-Patil said in CMD commands execution from the qt application:

      command.split(" ")

      Your splitting by space here and therefore "G:\Install and Software\Arduino\hardware\tools\avr/bin/avrdude" will be passed as two separate arguments to cmd which can not work.
      You can try to use QProcess::start(const QString &command) which maybe parses your string correct or have to enhance your splitting function by yourself.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply 20 Aug 2018, 09:28
      4
      • P Offline
        P Offline
        Pranit Patil
        wrote on 20 Aug 2018, 09:26 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • C Christian Ehrlicher
          17 Aug 2018, 17:34

          @Pranit-Patil said in CMD commands execution from the qt application:

          command.split(" ")

          Your splitting by space here and therefore "G:\Install and Software\Arduino\hardware\tools\avr/bin/avrdude" will be passed as two separate arguments to cmd which can not work.
          You can try to use QProcess::start(const QString &command) which maybe parses your string correct or have to enhance your splitting function by yourself.

          P Offline
          P Offline
          Pranit Patil
          wrote on 20 Aug 2018, 09:28 last edited by
          #4

          @Christian-Ehrlicher Thank you sir
          your suggestion is correct because of my split function cmd not working properly.
          (In my case if i manipulated my cmd like this -G:\InstallSoftware\Arduino\hardware\tools\avr/bin/avrdude) its working.
          but its not proper way.

          Im trying to understand QProcess::start(const QString &command) from QT Documents but not getting how to implement / use in my Case.

          if you have idea on it please suggest.
          Thank you sir.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 20 Aug 2018, 17:33 last edited by
            #5

            Just pass the complete command as a single QString and see if it can be parsed.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3

            1/5

            17 Aug 2018, 15:27

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved