Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

CMD commands execution from the qt application



  • 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


  • Lifetime Qt Champion

    @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.



  • This post is deleted!


  • @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.


  • Lifetime Qt Champion

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


Log in to reply