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. problem in run external program with QProcess
Qt 6.11 is out! See what's new in the release blog

problem in run external program with QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 3 Posters 2.1k 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.
  • S Offline
    S Offline
    saeid0034
    wrote on last edited by aha_1980
    #1

    hi i use this code to run some program with Argument with QProcess

    void Manager::arch()
    {
    	QTemporaryDir temporaryDir;
    	temporaryDir.setAutoRemove(false);
    
    	if (temporaryDir.isValid())
    	{
    		const QString exename = temporaryDir.path() + "/some.exe";
    		if (QFile::copy(":/new/some.exe", exename))
    		{
    			QString arg = QString("/list.txt");
    			QString loc = QString(QCoreApplication::applicationDirPath() + "/2/out.arc");
    			BigPAKC = new QProcess;
    			BigPAKC->start(temporaryDir.path() + "/some.exe", QStringList() << loc << "-Create=\"" + arg + "\"" << "-compress");
    			 
    			if (!BigPAKC->waitForStarted())
    				qDebug() << "started";
    	         
    			if (!BigPAKC->waitForFinished(-1))
    				qDebug() << "Finished";
    			 
    			qDebug() << BigPAKC->errorString();
    		}
    	}
    
    

    but my problem is both waitForStarted and waitForFinished never work, i mean waitForFinished doesnt wait for process to end and print Finished at start,
    also BigPAKC->errorString() give me Unknown error
    what is my problem?
    thanks

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Di you actually check if the executable you want to start is really there?

      btw: which this useless conversion all over the place: .toLatin1().data();?
      btw2: you're leaking the QProcess object - no need to create it on the heap here.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      2
      • S saeid0034

        hi i use this code to run some program with Argument with QProcess

        void Manager::arch()
        {
        	QTemporaryDir temporaryDir;
        	temporaryDir.setAutoRemove(false);
        
        	if (temporaryDir.isValid())
        	{
        		const QString exename = temporaryDir.path() + "/some.exe";
        		if (QFile::copy(":/new/some.exe", exename))
        		{
        			QString arg = QString("/list.txt");
        			QString loc = QString(QCoreApplication::applicationDirPath() + "/2/out.arc");
        			BigPAKC = new QProcess;
        			BigPAKC->start(temporaryDir.path() + "/some.exe", QStringList() << loc << "-Create=\"" + arg + "\"" << "-compress");
        			 
        			if (!BigPAKC->waitForStarted())
        				qDebug() << "started";
        	         
        			if (!BigPAKC->waitForFinished(-1))
        				qDebug() << "Finished";
        			 
        			qDebug() << BigPAKC->errorString();
        		}
        	}
        
        

        but my problem is both waitForStarted and waitForFinished never work, i mean waitForFinished doesnt wait for process to end and print Finished at start,
        also BigPAKC->errorString() give me Unknown error
        what is my problem?
        thanks

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @saeid0034
        There will be an error starting that process.
        You might find handling QProcess:errorOccurred gives a different error message.
        As I have also said before, I am unsure what the consequences of passing "-Create=\"" + arg + "\"" as a QProcess argument will be.
        You should also read standard error here, not sure if on PC there's a chance something gets written there if process can't be started?
        Is it OK to execute from that temporary directory?
        Can your exe find its DLLs?
        Etc.
        Start with something simpler than your exe :)

        1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          Di you actually check if the executable you want to start is really there?

          btw: which this useless conversion all over the place: .toLatin1().data();?
          btw2: you're leaking the QProcess object - no need to create it on the heap here.

          S Offline
          S Offline
          saeid0034
          wrote on last edited by saeid0034
          #4

          @Christian-Ehrlicher yes exe are in temp folder, actually program run without any problem and do the job, but QProcess signal doesn't work (i try to use finished signal too but its never get any signal)
          and about useless conversion, yes i edit my code
          and for object too

          @JonB i try to get error with

          QObject::connect(BigPAKC, &QProcess::errorOccurred, [] (QProcess::ProcessError error) {
                                      qDebug() << "Error: " << error;
                                  });
          

          but its doesn't work
          also im use "-Create=\"" + arg + "\"" for some reason, reason is maybe some user have some space in there folders address (in above code i just put location statically but in final code program get location from QCoreApplication::applicationDirPath() as you know if any space are in location, exe cant find correct location

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @saeid0034 said in problem in run extrnal program with QProcess:

            but QProcess signal doesn't work

            Your example code does not use any signals or slots!

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply
            0
            • S saeid0034

              @Christian-Ehrlicher yes exe are in temp folder, actually program run without any problem and do the job, but QProcess signal doesn't work (i try to use finished signal too but its never get any signal)
              and about useless conversion, yes i edit my code
              and for object too

              @JonB i try to get error with

              QObject::connect(BigPAKC, &QProcess::errorOccurred, [] (QProcess::ProcessError error) {
                                          qDebug() << "Error: " << error;
                                      });
              

              but its doesn't work
              also im use "-Create=\"" + arg + "\"" for some reason, reason is maybe some user have some space in there folders address (in above code i just put location statically but in final code program get location from QCoreApplication::applicationDirPath() as you know if any space are in location, exe cant find correct location

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @saeid0034
              Here's an example: Your code extracts from resource to file exe, then tries to execute --- maybe the virus protection doesn't like this, or has the exe open?

              Simplify your problem. Stop the extraction. Do it elsewhere than the temp directory. Don't pass a command line with embedded double-quotes in arguments. Try, say, cmd /c dir. Then we will know which bit is the problem.....

              1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @saeid0034 said in problem in run extrnal program with QProcess:

                but QProcess signal doesn't work

                Your example code does not use any signals or slots!

                S Offline
                S Offline
                saeid0034
                wrote on last edited by saeid0034
                #7

                @Christian-Ehrlicher i know because for now code itself doesnt work good

                QObject::connect(BigPAKC, &QProcess::errorOccurred, [] (QProcess::ProcessError error) {
                                            qDebug() << "Error: " << error;
                                        });
                QObject::connect(BigPAKC, &QProcess::started, [] () -> void {
                                            qDebug() << "Process has started!";
                                        });
                QObject::connect(BigPAKC, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                                         [=](int exitCode, QProcess::ExitStatus exitStatus){
                                            qDebug() << exitCode << exitStatus;
                                        });
                
                QObject::connect(BigPAKC, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &QObject::deleteLater);
                
                

                @JonB as i said program run without problem and do the job (also im sure antivirus dont block process) my only problem is signal doesn't emit

                JonBJ 1 Reply Last reply
                0
                • S saeid0034

                  @Christian-Ehrlicher i know because for now code itself doesnt work good

                  QObject::connect(BigPAKC, &QProcess::errorOccurred, [] (QProcess::ProcessError error) {
                                              qDebug() << "Error: " << error;
                                          });
                  QObject::connect(BigPAKC, &QProcess::started, [] () -> void {
                                              qDebug() << "Process has started!";
                                          });
                  QObject::connect(BigPAKC, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                                                           [=](int exitCode, QProcess::ExitStatus exitStatus){
                                              qDebug() << exitCode << exitStatus;
                                          });
                  
                  QObject::connect(BigPAKC, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &QObject::deleteLater);
                  
                  

                  @JonB as i said program run without problem and do the job (also im sure antivirus dont block process) my only problem is signal doesn't emit

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @saeid0034
                  Show your code now. Don't mix waitFor...()s with signals.

                  S 1 Reply Last reply
                  3
                  • JonBJ JonB

                    @saeid0034
                    Show your code now. Don't mix waitFor...()s with signals.

                    S Offline
                    S Offline
                    saeid0034
                    wrote on last edited by
                    #9

                    @JonB thanks, so this time i remove waitFor...() from my code and only use signal and slot, this time Finished Signal emited, with 0 QProcess::NormalExit, but i get unknow error too

                    JonBJ 1 Reply Last reply
                    0
                    • S saeid0034

                      @JonB thanks, so this time i remove waitFor...() from my code and only use signal and slot, this time Finished Signal emited, with 0 QProcess::NormalExit, but i get unknow error too

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @saeid0034
                      Better :) Don't use waitFor...()s again :) "unknown error" can also maybe if no error. Put in slots on readyReadStandardOutput()/Error() now?

                      S 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @saeid0034
                        Better :) Don't use waitFor...()s again :) "unknown error" can also maybe if no error. Put in slots on readyReadStandardOutput()/Error() now?

                        S Offline
                        S Offline
                        saeid0034
                        wrote on last edited by
                        #11

                        @JonB both give me empty output

                        BigPAKC->readAllStandardOutput();
                        BigPAKC->readAllStandardError();
                        
                        JonBJ 1 Reply Last reply
                        0
                        • S saeid0034

                          @JonB both give me empty output

                          BigPAKC->readAllStandardOutput();
                          BigPAKC->readAllStandardError();
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @saeid0034
                          I already suggested you try cmd /c dir but you would not. So far as I can see everything is working fine, you have not shown there is any actual problem.

                          You don't show code, so I can only hope your code is correctly in slots set up in the right places.

                          S 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @saeid0034
                            I already suggested you try cmd /c dir but you would not. So far as I can see everything is working fine, you have not shown there is any actual problem.

                            You don't show code, so I can only hope your code is correctly in slots set up in the right places.

                            S Offline
                            S Offline
                            saeid0034
                            wrote on last edited by
                            #13

                            @JonB thanks, yes code seems working
                            i send my code above, it all code i use for qprocess.
                            and about cmd /c dir, what i'm supposed to do with it?

                            JonBJ 1 Reply Last reply
                            0
                            • S saeid0034

                              @JonB thanks, yes code seems working
                              i send my code above, it all code i use for qprocess.
                              and about cmd /c dir, what i'm supposed to do with it?

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #14

                              @saeid0034 said in problem in run external program with QProcess:

                              thanks, yes code seems working

                              If you mean everything is working then there is no issue,

                              i send my code above, it all code i use for qprocess.

                              You have shown fragments of code, which you say you have added to and deleted from. I do not know what your code looks like now.

                              and about cmd /c dir, what i'm supposed to do with it?

                              You were supposed to replace your process start()ing code by:
                              BigPAKC->start("cmd", QStringList() << "/c" << "dir");

                              EDIT
                              OMG, I have only just noticed in your code:

                              			if (!BigPAKC->waitForStarted())
                              				qDebug() << "started";
                              	         
                              			if (!BigPAKC->waitForFinished(-1))
                              				qDebug() << "Finished";
                              

                              I did not see the !s. So you print nothing when all is well.... So what is your problem if it all worked fine from the outset?

                              S 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @saeid0034 said in problem in run external program with QProcess:

                                thanks, yes code seems working

                                If you mean everything is working then there is no issue,

                                i send my code above, it all code i use for qprocess.

                                You have shown fragments of code, which you say you have added to and deleted from. I do not know what your code looks like now.

                                and about cmd /c dir, what i'm supposed to do with it?

                                You were supposed to replace your process start()ing code by:
                                BigPAKC->start("cmd", QStringList() << "/c" << "dir");

                                EDIT
                                OMG, I have only just noticed in your code:

                                			if (!BigPAKC->waitForStarted())
                                				qDebug() << "started";
                                	         
                                			if (!BigPAKC->waitForFinished(-1))
                                				qDebug() << "Finished";
                                

                                I did not see the !s. So you print nothing when all is well.... So what is your problem if it all worked fine from the outset?

                                S Offline
                                S Offline
                                saeid0034
                                wrote on last edited by
                                #15

                                @JonB Im worried about Unknown error but as you say its possible its mean no error

                                JonBJ 1 Reply Last reply
                                0
                                • S saeid0034

                                  @JonB Im worried about Unknown error but as you say its possible its mean no error

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @saeid0034

                                  QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().

                                  No error occurred in your code. This has been a long discussion for that situation.

                                  Why in the world did you write

                                  			if (!BigPAKC->waitForStarted())
                                  				qDebug() << "started";
                                  

                                  instead of if (BigPAKC->waitForStarted()) ?

                                  S 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @saeid0034

                                    QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().

                                    No error occurred in your code. This has been a long discussion for that situation.

                                    Why in the world did you write

                                    			if (!BigPAKC->waitForStarted())
                                    				qDebug() << "started";
                                    

                                    instead of if (BigPAKC->waitForStarted()) ?

                                    S Offline
                                    S Offline
                                    saeid0034
                                    wrote on last edited by
                                    #17

                                    @JonB because i see example on https://doc.qt.io/qt-5/qprocess.html

                                    JonBJ 1 Reply Last reply
                                    0
                                    • S saeid0034

                                      @JonB because i see example on https://doc.qt.io/qt-5/qprocess.html

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @saeid0034
                                      Your code:

                                      			if (!BigPAKC->waitForStarted())
                                      				qDebug() << "started";
                                      	         
                                      			if (!BigPAKC->waitForFinished(-1))
                                      				qDebug() << "Finished";
                                      

                                      Your description of what was wrong:

                                      but my problem is both waitForStarted and waitForFinished never work, i mean waitForFinished doesnt wait for process to end and print Finished at start,

                                      Why do you expect a successful waitForFinished() to print Finished here? Do you understand that with your use of ! it won't say anything when everything succeeds as it should?

                                      S 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @saeid0034
                                        Your code:

                                        			if (!BigPAKC->waitForStarted())
                                        				qDebug() << "started";
                                        	         
                                        			if (!BigPAKC->waitForFinished(-1))
                                        				qDebug() << "Finished";
                                        

                                        Your description of what was wrong:

                                        but my problem is both waitForStarted and waitForFinished never work, i mean waitForFinished doesnt wait for process to end and print Finished at start,

                                        Why do you expect a successful waitForFinished() to print Finished here? Do you understand that with your use of ! it won't say anything when everything succeeds as it should?

                                        S Offline
                                        S Offline
                                        saeid0034
                                        wrote on last edited by
                                        #19

                                        @JonB sorry about that, i only see unknow error -_-
                                        thanks about help

                                        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