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. QProcess startDetached can open the exe but start cannot
Forum Updated to NodeBB v4.3 + New Features

QProcess startDetached can open the exe but start cannot

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 10.2k 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.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by tham
    #1

    Qt5.7
    mingw5.3 32bits

    startDetached work

    bool const can_open =
               QProcess::startDetached("\"" + app_dir_path + "/auto_updater/auto_updater\"",
                                       QStringList()<<"-n");
       qDebug()<<"app can open : "<<can_open;
    

    But start do not, error type is failed to open

    process.start("\"" + app_dir_path + "/auto_updater/auto_updater\"",
                      QStringList()<<"-n",
                      QIODevice::ReadOnly);
    if(process.waitForFinished(-1)){
            //....do something
        }else{
            //will reach here
            qDebug()<<"cannot wait for finished : "<<process.state();
        }
    

    I use QFile to check the exe exist or not, the exe could be opened

    {
            QFile file(app_dir_path + "/auto_updater/auto_updater.exe");
            file.open(QIODevice::ReadOnly);
            qDebug()<<"auto updater can open : "<<file.isOpen();
     }
    

    The path are absolute path

    Do anyone know what is happening?How could I fix the issues?Thanks

    1 Reply Last reply
    0
    • RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by Ratzz
      #2

      @tham
      have you tried creating object ?

      QProcess *myProcess = new QProcess(parent);
      myProcess->start(program, arguments);
      

      http://doc.qt.io/qt-5/qprocess.html#details
      http://stackoverflow.com/questions/18074826/qprocess-fails-to-execute-external-executable
      https://forum.qt.io/topic/71296/qprocess-is-not-working-in-linux/3

      --Alles ist gut.

      thamT 1 Reply Last reply
      0
      • RatzzR Ratzz

        @tham
        have you tried creating object ?

        QProcess *myProcess = new QProcess(parent);
        myProcess->start(program, arguments);
        

        http://doc.qt.io/qt-5/qprocess.html#details
        http://stackoverflow.com/questions/18074826/qprocess-fails-to-execute-external-executable
        https://forum.qt.io/topic/71296/qprocess-is-not-working-in-linux/3

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

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

        have you tried creating object ?

        Yes, I tried to put it on heap before(for reading error message because object on stack will die after it leave the scope), but this cannot work either.

        I try to search the solution by google, change it to native path, add """, use absolute path, but none of them work.

        1 Reply Last reply
        0
        • 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 Extensions
                                  • Unsolved