How to set working directory of QProcess

Heads up! You've already completed this tutorial.

Young-Ho_Song | 2020-12-30 06:49:22 UTC | #1

Hi. Thank you very much for the nice tutorial about running external program in Qt. I would like to ask a question about QProcess. If the external program is in different folder of the Qt code, how should one call an external program in its own directory?

Suppose external program is in 'C:/program_folder/test.exe' (with its own input files in that directory.) and the Qt code is in 'C:/qt_source/test.py' I tried

python
p = QProcess()
p.setWorkingDirectory('C:/program_folder/')
p.start('test.exe')

But, it did not work. Could you help me for this problem?

Thank you.


Young-Ho_Song | 2021-01-06 13:57:07 UTC | #2

I found a solution. The problem semes to be from inconsistent use of setWorkingDirectory() and start(). After changing the code as follows, setWorkingDirectory() works.

python
p = QProcess()
p.setProgram('C:/program_folder/test.exe')
p.setWorkingDirectory("C:/program_folder")
p.start()

martin | 2021-01-06 13:56:55 UTC | #3

Thanks for posting the solution @Young-Ho_Song -- I was out of the office over Christmas so missed this til now.

I wonder if this is related to the p.start('program') method now being deprecated?

If you look at the Qt documentation for start you'll see you now must pass in the program name and the arguments as a separate list. For no arguments, you pass an empty list. I should make this clearer in the tutorial I think.

The method with only the program name -- where you could pass the arguments in the same string -- is deprecated.


Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak