Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

problem in run external program with QProcess



  • 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


  • Lifetime Qt Champion

    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.



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



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


  • Lifetime Qt Champion

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



  • @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.....



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



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



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



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



  • @JonB both give me empty output

    BigPAKC->readAllStandardOutput();
    BigPAKC->readAllStandardError();
    


  • @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.



  • @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?



  • @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?



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



  • @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()) ?



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



  • @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?



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


Log in to reply