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. How to run external executable in QT Project?
Forum Updated to NodeBB v4.3 + New Features

How to run external executable in QT Project?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.7k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    aravmadd
    wrote on last edited by
    #1

    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 :-)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which issues do you have with QProcess ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Which issues do you have with QProcess ?

        A Offline
        A Offline
        aravmadd
        wrote on last edited by aravmadd
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          3
          • SGaistS SGaist

            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.

            A Offline
            A Offline
            aravmadd
            wrote on last edited by
            #5

            @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 :-)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Where is Process.exe located exactly ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply
              0
              • SGaistS SGaist

                Where is Process.exe located exactly ?

                A Offline
                A Offline
                aravmadd
                wrote on last edited by aravmadd
                #7

                @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";

                1 Reply Last reply
                0

                • Login

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