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.

The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]

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.


Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

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

How to set working directory of QProcess was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.