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

How to run external executable in QT Project?



  • Hi!

    I created a gui with one push button and several line Edits. When user hits push button, I wanted to read all the inputs from other lineEdit boxes and run executable with this inputs as an arguments.

    For example i have one executable which takes few arguments and do somethings and give output.

    So I need to run this executable when someone hits push button with arguments .

    But from my search i found that QProcess needs to be used but i was not able to find any success in using QProcess.

    Can someone help me in this?

    Thanks in Advance :-)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Which issues do you have with QProcess ?



  • @SGaist This is my code when pushbutton is pressed. i wanted to run one executable inside else condition but it is not doing what i am planning to do.

    I also tried Process detached but i am not sure if it is doing something!

    I also https://forum.qt.io/topic/96231/how-do-i-run-an-exe-file-on-button-clicked-in-a-qt-app/4 followed this one but was not able to successfully implement or I am doing wrong somewhere?

    void Widget::on_pushButton_pressed()
    {
    
    
        ui->plainTextEdit->appendHtml("<div style='color: green;'> Process Started with below Selected Parameters </div>");
        if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty() || ui->lineEdit_3->text().isEmpty() || ui->lineEdit_4->text().isEmpty() )
        {
            ui->plainTextEdit->appendHtml("<div style='color: red;'> Process Failed due to some parameters missing. Please Check all the parameters needed Except in Check boxes and DropDown Menu </div>");
            ui->plainTextEdit->appendHtml("<div style='color: red;'> By Default CheckBoxes are disabled. Please Check the respective box if you want to enable </div>");
    
    
        }
        else
        {
            QString i1 = ui->lineEdit->text();
            QString i2 = ui->lineEdit_2->text();
            QString it1 = ui->lineEdit_3->text();
            QString it2 = ui->lineEdit_4->text();
            bool enaout = ui->checkBox->isChecked();
            QString res;
            if (enaout == true)
                res = "Output Enabled";
            else
                res = "Output Disabled";
    
            bool ra = ui->checkBox_2->isChecked();
            QString res1;
            if (ra == true)
                res1 = "RA is  Enabled";
            else
                res1 = "RA is Disabled";
    
            ui->plainTextEdit->appendHtml("i1:" + i1);
            ui->plainTextEdit->appendHtml("i2: " + i2);
            ui->plainTextEdit->appendHtml("it1: "+ it1);
            ui->plainTextEdit->appendHtml("it2: " + it2);
            ui->plainTextEdit->appendPlainText(res);
            ui->plainTextEdit->appendPlainText(res1);
    
    
            QString fileName = "Process.exe";
            if(QFileInfo(fileName).exists())
            {
                QStringList arguments;
                arguments << ui->lineEdit->text()<<ui->lineEdit_2->text();
    
                proc = new QProcess();
    
                proc->start(fileName,arguments);
                if(proc->state() != QProcess::NotRunning)
                {
                    proc->waitForFinished();
                    
                    QString result = proc->readAllStandardOutput();
                    ui->plainTextEdit->appendPlainText(result);
    
                }
                
                else{
                    ui->plainTextEdit->appendPlainText("Unable to find file with this name");
                }
                
    
            }
    
            else
                ui->plainTextEdit->appendHtml("<div style='color: red;'> Error While Opening Executable </div>");
    
            
        }
    }
    
    /*
    
    QProcess *proc was initialized in header and 
    proc = new Process is used in constructor.
    
    so in the above code i am directly using proc->somestuff
    
    
    */
    
    
    
    

    Also I would like to capture output or error messages in cmd shell and send to qt plainTextEdit object. Currently also trying this?. It would be great if also someone can give suggestion on this!


  • Lifetime Qt Champion

    As most of the time with QProcess: build the parameters as a list and pass it separately as explained in the documentation.

    Then, just below the example, there's an explanation about communication channels.

    Next, you should do error checking. You do not check whether your process started successfully.

    Finally, you are leaking QProcess objects since you create on new on every call of that function without deleting it ever. Granted it has a parent but that only means it will be automatically deleted once the parent is deleted.



  • @SGaist Hi! Thanks for your valuable suggestions. Can you please give a look into above-changed code? I tried to implement what you suggested. As per my understanding, I believe I implemented suggested modifications by you.

    But however, every time it is unable to find my executable and throws me an error. I am giving a look into fix this. But can you please look above code and let me know if it is wrong or right?

    Many Thanks :-)


  • Lifetime Qt Champion

    Where is Process.exe located exactly ?



  • @SGaist Hi! Its in build directory.

    The reason for keeping there is under my project build settings it is showing build directory. So I thought if i keep executable there so that it can easily find the executable.

    Update Solved by using Qstring fileName = QDir::currentPath()+"/Process.exe";


Log in to reply