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. Run external program
QtWS25 Last Chance

Run external program

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 3.9k 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.
  • A Offline
    A Offline
    aminemaar
    wrote on last edited by
    #1

    Hi,
    I want to run external application in my Qtquick application,
    I used this code but it's not working

    QProcess*   process = new QProcess();
    QString program = "C:\\WTB\\Serveur\\svr.exe";
    process->start(program);
    
    Pablo J. RoginaP 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Hook up error handler and see what it says.

      connect(process , &QProcess::errorOccurred, [=](QProcess::ProcessError error) 
      { 
          qDebug() << "error enum val = " << error << endl; 
      });
      
      1 Reply Last reply
      5
      • A aminemaar

        Hi,
        I want to run external application in my Qtquick application,
        I used this code but it's not working

        QProcess*   process = new QProcess();
        QString program = "C:\\WTB\\Serveur\\svr.exe";
        process->start(program);
        
        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @aminemaar said in Run external program:

        my Qtquick application

        but you're showing C++ code, am I missing something?

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #4

          Hello! To start the external program, you can also check the startDetached (https://doc.qt.io/qt-5/qprocess.html#startDetached) and execute (https://doc.qt.io/qt-5/qprocess.html#execute) methods. You can even use the static QProcess::startDetached("calc.exe"); method.

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

            @Cobra91151 The various detached methods won't help. Here @aminemaar needs to discover why its application is not running.

            @aminemaar beside what @mrjj already wrote. What does your application do ? From the name it looks like some kind of network server ? In any case, did you also consider using the waitForStarted method ? This will allow you to know if the application started and in any case, check its status and potential error.

            On a side note, doing it like that, you are leaking the QProcess object. How are you planning to handle it ?

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

            1 Reply Last reply
            4
            • Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by Cobra91151
              #6

              @SGaist

              Yes, I agree. So, how about this:

              Code:

              QProcess *appProcess = new QProcess();
                  connect(appProcess, &QProcess::started, [appProcess]() {
                      qDebug() << "Started!";
                      qDebug() << "PID: " << appProcess->processId();
                      qDebug() << "Process state: " << appProcess->state();
                  });
                  connect(appProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), [appProcess](int exitCode, QProcess::ExitStatus exitStatus) {
                      qDebug() << exitCode;
                      qDebug() << exitStatus;
                      appProcess->deleteLater(); //clear the QProcess object
                  });
                  appProcess->start("calc");
              

              It will check the process state/pid, exit code and status and then delete the object using deleteLater() method.

              Also @aminemaar can try the native Win API function combined with Qt:

              HINSTANCE startApp(bool runAppAsAdmin, QString appPath, QString appArguments)
              {
                  HINSTANCE hResult;
              
                  if (runAppAsAdmin) {
                      hResult = ShellExecute(nullptr, L"runas", appPath.toStdWString().c_str(), appArguments.toStdWString().c_str(), nullptr, SW_SHOW);
                  } else {
                      hResult = ShellExecute(nullptr, nullptr, appPath.toStdWString().c_str(), appArguments.toStdWString().c_str(), nullptr, SW_SHOW);
                  }
              
                  return hResult;
              }
              
              1 Reply Last reply
              3

              • Login

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