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.


1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) More info 120 mins ($375)

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

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.

Interested in contributing to the site? Find out more.