

Self.p = QProcess() # Keep a reference to the QProcess (e.g. Python from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QPlainTextEdit, Pressing the push button calls our custom slot start_process, in which we'll execute our external process. This is shown below - a simple window with a QPushButton and QTextArea. To experiment with running programs through QProcess we need a skeleton application.

Now we have our dummy_script.py we can run it from within our Qt application. Later we'll see how to extract data from this output. This simulates a long-running external program which is printing out periodic status messages. If you have an existing command line tool you'd like to test with, you can substitute that instead.ĭon't worry too much about the contents of this script, it's just a series of print (stream write) statements with a half second wait after. I'm using Python here to be sure it works on all platforms. Put the following in a file, and save it with the name dummy_script.py. Here we'll create a simple Python script for that purpose, which we can then launch from within our application. To be able to test running external programs with QProcess we need to have something to run. In this tutorial we'll look at QProcess, the Qt system for running external programs from within your own app. This works great when using Python libraries to accomplish tasks, but sometimes you want to run external applications, passing parameters and getting the results. Stdout get updated every 0.5 seconds for every two lines to contain: 0Īnd each log file contains the respective log for a given process.So far we've looked at how to run work in separate threads, allowing you to do complex tasks without interrupting your UI. T2 = threading.Thread(target=output_reader, args=(proc2, file2)) T1 = threading.Thread(target=output_reader, args=(proc1, file1)) Subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc2, \

With subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc1, \ The threading module allows us to do that.įirst, have a look at how to do the output redirection part alone in this question: Python Popen: Write to stdout AND log file simultaneously For example, I wanted to launch two processes that talk over a port between them, and save their stdout to a log file and stdout. However, there are cases where you need this. Both capture output and run on background with threadingĪs mentioned on this answer, if you capture the output with stdout= and then try to read(), then the process blocks.
