Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess startDetached can open the exe but start cannot
Qt 6.12 LTS is out! See what's new in the release blog

QProcess startDetached can open the exe but start cannot

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 12.5k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Hi,

    Before waitForFinished, you should check waitForStarted and also the check the content of stdout and stderr like discussed here.

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

    thamT 1 Reply Last reply
    1
    • SGaistS SGaist

      Hi,

      Before waitForFinished, you should check waitForStarted and also the check the content of stdout and stderr like discussed here.

      thamT Offline
      thamT Offline
      tham
      wrote on last edited by tham
      #5

      @SGaist

      Thanks, I tried as you suggest, but it cannot work either.

      process.start((app_dir_path + "/auto_updater/auto_updater"),
                        QStringList()<<"-n",
                        QIODevice::ReadOnly);
          if(process.waitForStarted(-1) && process.waitForFinished(-1)){
              //the process never start
              QString const process_output(process.readAll());
      
              qDebug()<<"process output : "<<process_output;
              if(process_output.contains("need to update")){
                  return true;
              }else{
                  return false;
              }
          }else{
              //still come here
              qDebug()<<"cannot wait for finished : "<<process.state();
          }
      

      I tried to allocated the memory on heap and connect the errorOccured signal, the error message is "QProcess::FailedToStart"

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

        You shouldn't test for both waitForStared and waitForFinished in the same if, they represent different states. Your application can fail to start for different reason that it fails to finish.

        Also, you should read from readAllStandardError and readAllStandardOutput.

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

        thamT 1 Reply Last reply
        1
        • SGaistS SGaist

          You shouldn't test for both waitForStared and waitForFinished in the same if, they represent different states. Your application can fail to start for different reason that it fails to finish.

          Also, you should read from readAllStandardError and readAllStandardOutput.

          thamT Offline
          thamT Offline
          tham
          wrote on last edited by tham
          #7

          @SGaist
          Thanks for your suggestion, but it still cannot open the exe(original codes works perfect if I use Qt5.6.1).

          QProcess process;
          process.start((app_dir_path + "/auto_updater/auto_updater"),
                            QStringList()<<"-n",
                            QIODevice::ReadOnly);
              if(!process.waitForStarted(-1)){
                  //come here
                  qDebug()<<"cannot start";
                  return false;
              }
          
              if(process.waitForFinished(-1)){
                  QString const process_output(process.readAllStandardOutput());
          
                  qDebug()<<"process output : "<<process_output;
                  if(process_output.contains("need to update")){
                      return true;
                  }else{
                      return false;
                  }
              }else{        
                  qDebug()<<"cannot wait for finished : "<<process.state();
              }
          

          ps : I test waitForStarted separately before, put it in the same if to simplify the example, sorry if this confuse you.I connect to the signal errorOccured too(in this case, I allocate QProcess on heap, memory is free or not is not a big deal since this is dirty codes for debug purpose only), the error is "QProcess::FailedToStart".

          kshegunovK 1 Reply Last reply
          0
          • thamT tham

            @SGaist
            Thanks for your suggestion, but it still cannot open the exe(original codes works perfect if I use Qt5.6.1).

            QProcess process;
            process.start((app_dir_path + "/auto_updater/auto_updater"),
                              QStringList()<<"-n",
                              QIODevice::ReadOnly);
                if(!process.waitForStarted(-1)){
                    //come here
                    qDebug()<<"cannot start";
                    return false;
                }
            
                if(process.waitForFinished(-1)){
                    QString const process_output(process.readAllStandardOutput());
            
                    qDebug()<<"process output : "<<process_output;
                    if(process_output.contains("need to update")){
                        return true;
                    }else{
                        return false;
                    }
                }else{        
                    qDebug()<<"cannot wait for finished : "<<process.state();
                }
            

            ps : I test waitForStarted separately before, put it in the same if to simplify the example, sorry if this confuse you.I connect to the signal errorOccured too(in this case, I allocate QProcess on heap, memory is free or not is not a big deal since this is dirty codes for debug purpose only), the error is "QProcess::FailedToStart".

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #8

            @tham

            QProcess * process = new QProcess;
            QObject::connect(process, &QProcess::errorOccurred, [] (QProcess::ProcessError error) -> void {
                qDebug() << "Error: " << error;
            });
            QObject::connect(process, &QProcess::started, [] () -> void {
                qDebug() << "Process has started!";
            });
            QObject::connect(process, &QProcess::finished, [process] () -> void {
                qDebug() << "Process has finished!";
                qDebug() << process->readAllStandardOutput();
            });
            QObject::connect(process, &QProcess::finished, process, &QObject::deleteLater);
            

            As a rule of thumb avoid the the waitFor* functions.
            ... and please handle the errors you might be getting.

            Read and abide by the Qt Code of Conduct

            thamT 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @tham

              QProcess * process = new QProcess;
              QObject::connect(process, &QProcess::errorOccurred, [] (QProcess::ProcessError error) -> void {
                  qDebug() << "Error: " << error;
              });
              QObject::connect(process, &QProcess::started, [] () -> void {
                  qDebug() << "Process has started!";
              });
              QObject::connect(process, &QProcess::finished, [process] () -> void {
                  qDebug() << "Process has finished!";
                  qDebug() << process->readAllStandardOutput();
              });
              QObject::connect(process, &QProcess::finished, process, &QObject::deleteLater);
              

              As a rule of thumb avoid the the waitFor* functions.
              ... and please handle the errors you might be getting.

              thamT Offline
              thamT Offline
              tham
              wrote on last edited by tham
              #9

              @kshegunov

              First of all, thanks for your helps, but this do not solve the problem.

              As a rule of thumb avoid the the waitFor* functions.

              I know what you mean(avoid gui freezing), but in my case, waitFor* is a right choice. Especially suit for a simple example.

              ... and please handle the errors you might be getting.

              I do connect the signal of errorOccured as I mentioned before and write down what kind of error I get, I did not include them in the source codes because this could make the example easier to read.

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

                What do you get from reading from stdout and stderr ?

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

                thamT 1 Reply Last reply
                0
                • thamT tham

                  @kshegunov

                  First of all, thanks for your helps, but this do not solve the problem.

                  As a rule of thumb avoid the the waitFor* functions.

                  I know what you mean(avoid gui freezing), but in my case, waitFor* is a right choice. Especially suit for a simple example.

                  ... and please handle the errors you might be getting.

                  I do connect the signal of errorOccured as I mentioned before and write down what kind of error I get, I did not include them in the source codes because this could make the example easier to read.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #11

                  @tham said in QProcess startDetached can open the exe but start cannot:

                  but in my case, waitFor* is a right choice.

                  Almost never is, but let's leave that discussion for some other time.

                  @tham said in QProcess startDetached can open the exe but start cannot:

                  error type is failed to open

                  Then the file can't be found. Write down the full path you're attempting to start, make sure it is an executable (it has -x for the user/group access) and you might also want to remove the quotes from the path.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What do you get from reading from stdout and stderr ?

                    thamT Offline
                    thamT Offline
                    tham
                    wrote on last edited by tham
                    #12

                    @SGaist said in QProcess startDetached can open the exe but start cannot:

                    What do you get from reading from stdout and stderr ?

                    Get nothing, because process.waitForStarted(-1) always return false(qDebug print "cannot start"), error message emit by errorOccured signal is "QProcess::FailedToStart". The weird part is

                    1 : startDetached can start the exe with the same path and same command(-n)
                    2 : start api of Qt5.6.1 work

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

                      But why don't you print the output of process.readAllStandardOutput and process.readAllStandardError after it failed to start ?

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

                      thamT 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        But why don't you print the output of process.readAllStandardOutput and process.readAllStandardError after it failed to start ?

                        thamT Offline
                        thamT Offline
                        tham
                        wrote on last edited by
                        #14

                        @SGaist said in QProcess startDetached can open the exe but start cannot:

                        But why don't you print the output of process.readAllStandardOutput and process.readAllStandardError after it failed to start ?

                        Tried it, output nothing

                        1 Reply Last reply
                        0
                        • thamT Offline
                          thamT Offline
                          tham
                          wrote on last edited by tham
                          #15

                          Looks like no one can solve this problem, I will open a bug issue. It is extremely weird that startDetached can start the exe but the start api can not start the exe + Qt5.6.1 work but Qt5.7 do not work.

                          1 Reply Last reply
                          0
                          • thamT Offline
                            thamT Offline
                            tham
                            wrote on last edited by
                            #16

                            I find out the problem, it is because the app need admin privilege to open it.

                            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
                            • Unsolved