QProcess¶
Synopsis¶
Functions¶
def
arguments
()def
closeReadChannel
(channel)def
closeWriteChannel
()def
environment
()def
error
()def
exitCode
()def
exitStatus
()def
inputChannelMode
()def
processChannelMode
()def
processEnvironment
()def
processId
()def
program
()def
readAllStandardError
()def
readAllStandardOutput
()def
readChannel
()def
setArguments
(arguments)def
setEnvironment
(environment)def
setInputChannelMode
(mode)def
setProcessChannelMode
(mode)def
setProcessEnvironment
(environment)def
setProcessState
(state)def
setProgram
(program)def
setReadChannel
(channel)def
setStandardErrorFile
(fileName[, mode=QIODevice.Truncate])def
setStandardInputFile
(fileName)def
setStandardOutputFile
(fileName[, mode=QIODevice.Truncate])def
setStandardOutputProcess
(destination)def
setWorkingDirectory
(dir)def
start
([mode=QIODevice.ReadWrite])def
start
(command[, mode=QIODevice.ReadWrite])def
start
(program, arguments[, mode=QIODevice.ReadWrite])def
startDetached
([pid=None])def
state
()def
waitForFinished
([msecs=30000])def
waitForStarted
([msecs=30000])def
workingDirectory
()
Virtual functions¶
def
setupChildProcess
()
Slots¶
Signals¶
def
error
(error)def
errorOccurred
(error)def
finished
(exitCode)def
finished
(exitCode, exitStatus)
Static functions¶
def
execute
(command)def
execute
(program, arguments)def
nullDevice
()def
startDetached
(command)def
startDetached
(program, arguments)def
startDetached
(program, arguments, workingDirectory)def
systemEnvironment
()
Detailed Description¶
Running a Process¶
To start a process, pass the name and command line arguments of the program you want to run as arguments to
start()
. Arguments are supplied as individual strings in aQStringList
.Alternatively, you can set the program to run with
setProgram()
andsetArguments()
, and then callstart()
oropen()
.For example, the following code snippet runs the analog clock example in the Fusion style on X11 platforms by passing strings containing “-style” and “fusion” as two items in the list of arguments:
QObject *parent; ... QString program = "./path/to/Qt/examples/widgets/analogclock"; QStringList arguments; arguments << "-style" << "fusion"; QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments);
QProcess
then enters theStarting
state, and when the program has started,QProcess
enters theRunning
state and emitsstarted()
.
QProcess
allows you to treat a process as a sequential I/O device. You can write to and read from the process just as you would access a network connection usingQTcpSocket
. You can then write to the process’s standard input by callingwrite()
, and read the standard output by callingread()
,readLine()
, andgetChar()
. Because it inheritsQIODevice
,QProcess
can also be used as an input source forQXmlReader
, or for generating data to be uploaded usingQNetworkAccessManager
.When the process exits,
QProcess
reenters theNotRunning
state (the initial state), and emitsfinished()
.The
finished()
signal provides the exit code and exit status of the process as arguments, and you can also callexitCode()
to obtain the exit code of the last process that finished, andexitStatus()
to obtain its exit status. If an error occurs at any point in time,QProcess
will emit theerrorOccurred()
signal. You can also callerror()
to find the type of error that occurred last, andstate()
to find the current process state.Note
QProcess
is not supported on VxWorks, iOS, tvOS, watchOS, or the Universal Windows Platform.
Communicating via Channels¶
Processes have two predefined output channels: The standard output channel (
stdout
) supplies regular console output, and the standard error channel (stderr
) usually supplies the errors that are printed by the process. These channels represent two separate streams of data. You can toggle between them by callingsetReadChannel()
.QProcess
emitsreadyRead()
when data is available on the current read channel. It also emitsreadyReadStandardOutput()
when new standard output data is available, and when new standard error data is available,readyReadStandardError()
is emitted. Instead of callingread()
,readLine()
, orgetChar()
, you can explicitly read all data from either of the two channels by callingreadAllStandardOutput()
orreadAllStandardError()
.The terminology for the channels can be misleading. Be aware that the process’s output channels correspond to
QProcess
‘s read channels, whereas the process’s input channels correspond toQProcess
‘s write channels. This is because what we read usingQProcess
is the process’s output, and what we write becomes the process’s input.
QProcess
can merge the two output channels, so that standard output and standard error data from the running process both use the standard output channel. CallsetProcessChannelMode()
withMergedChannels
before starting the process to activate this feature. You also have the option of forwarding the output of the running process to the calling, main process, by passingForwardedChannels
as the argument. It is also possible to forward only one of the output channels - typically one would useForwardedErrorChannel
, butForwardedOutputChannel
also exists. Note that using channel forwarding is typically a bad idea in GUI applications - you should present errors graphically instead.Certain processes need special environment settings in order to operate. You can set environment variables for your process by calling
setProcessEnvironment()
. To set a working directory, callsetWorkingDirectory()
. By default, processes are run in the current working directory of the calling process.The positioning and the screen Z-order of windows belonging to GUI applications started with
QProcess
are controlled by the underlying windowing system. For Qt 5 applications, the positioning can be specified using the-qwindowgeometry
command line option; X11 applications generally accept a-geometry
command line option.Note
On QNX, setting the working directory may cause all application threads, with the exception of the
QProcess
caller thread, to temporarily freeze during the spawning process, owing to a limitation in the operating system.
Synchronous Process API¶
QProcess
provides a set of functions which allow it to be used without an event loop, by suspending the calling thread until certain signals are emitted:
waitForStarted()
blocks until the process has started.
waitForReadyRead()
blocks until new data is available for reading on the current read channel.
waitForBytesWritten()
blocks until one payload of data has been written to the process.
waitForFinished()
blocks until the process has finished.Calling these functions from the main thread (the thread that calls
exec()
) may cause your user interface to freeze.The following example runs
gzip
to compress the string “Qt rocks!”, without an event loop:QProcess gzip; gzip.start("gzip", QStringList() << "-c"); if (!gzip.waitForStarted()) return false; gzip.write("Qt rocks!"); gzip.closeWriteChannel(); if (!gzip.waitForFinished()) return false; QByteArray result = gzip.readAll();
Notes for Windows Users¶
Some Windows commands (for example,
dir
) are not provided by separate applications, but by the command interpreter itself. If you attempt to useQProcess
to execute these commands directly, it won’t work. One possible solution is to execute the command interpreter itself (cmd.exe
on some Windows systems), and ask the interpreter to execute the desired command.
- class PySide2.QtCore.QProcess([parent=None])¶
- param parent:
Constructs a
QProcess
object with the givenparent
.
- PySide2.QtCore.QProcess.ProcessError¶
This enum describes the different types of errors that are reported by
QProcess
.Constant
Description
QProcess.FailedToStart
The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program.
QProcess.Crashed
The process crashed some time after starting successfully.
QProcess.Timedout
The last waitFor…() function timed out. The state of
QProcess
is unchanged, and you can try calling waitFor…() again.QProcess.WriteError
An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.
QProcess.ReadError
An error occurred when attempting to read from the process. For example, the process may not be running.
QProcess.UnknownError
An unknown error occurred. This is the default return value of
error()
.See also
- PySide2.QtCore.QProcess.ProcessState¶
This enum describes the different states of
QProcess
.Constant
Description
QProcess.NotRunning
The process is not running.
QProcess.Starting
The process is starting, but the program has not yet been invoked.
QProcess.Running
The process is running and is ready for reading and writing.
See also
- PySide2.QtCore.QProcess.ProcessChannel¶
This enum describes the process channels used by the running process. Pass one of these values to
setReadChannel()
to set the current read channel ofQProcess
.Constant
Description
QProcess.StandardOutput
The standard output (stdout) of the running process.
QProcess.StandardError
The standard error (stderr) of the running process.
See also
- PySide2.QtCore.QProcess.ProcessChannelMode¶
This enum describes the process output channel modes of
QProcess
. Pass one of these values tosetProcessChannelMode()
to set the current read channel mode.Constant
Description
QProcess.SeparateChannels
QProcess
manages the output of the running process, keeping standard output and standard error data in separate internal buffers. You can select theQProcess
‘s current read channel by callingsetReadChannel()
. This is the default channel mode ofQProcess
.QProcess.MergedChannels
QProcess
merges the output of the running process into the standard output channel (stdout
). The standard error channel (stderr
) will not receive any data. The standard output and standard error data of the running process are interleaved.QProcess.ForwardedChannels
QProcess
forwards the output of the running process onto the main process. Anything the child process writes to its standard output and standard error will be written to the standard output and standard error of the main process.QProcess.ForwardedErrorChannel
QProcess
manages the standard output of the running process, but forwards its standard error onto the main process. This reflects the typical use of command line tools as filters, where the standard output is redirected to another process or a file, while standard error is printed to the console for diagnostic purposes. (This value was introduced in Qt 5.2.)QProcess.ForwardedOutputChannel
Complementary to . (This value was introduced in Qt 5.2.)
Note
Windows intentionally suppresses output from GUI-only applications to inherited consoles. This does not apply to output redirected to files or pipes. To forward the output of GUI-only applications on the console nonetheless, you must use and do the forwarding yourself by reading the output and writing it to the appropriate output channels.
See also
- PySide2.QtCore.QProcess.InputChannelMode¶
This enum describes the process input channel modes of
QProcess
. Pass one of these values tosetInputChannelMode()
to set the current write channel mode.Constant
Description
QProcess.ManagedInputChannel
QProcess
manages the input of the running process. This is the default input channel mode ofQProcess
.QProcess.ForwardedInputChannel
QProcess
forwards the input of the main process onto the running process. The child process reads its standard input from the same source as the main process. Note that the main process must not try to read its standard input while the child process is running.See also
- PySide2.QtCore.QProcess.ExitStatus¶
This enum describes the different exit statuses of
QProcess
.Constant
Description
QProcess.NormalExit
The process exited normally.
QProcess.CrashExit
The process crashed.
See also
- PySide2.QtCore.QProcess.arguments()¶
- Return type:
list of strings
Returns the command line arguments the process was last started with.
See also
- PySide2.QtCore.QProcess.closeReadChannel(channel)¶
- Parameters:
channel –
ProcessChannel
Closes the read channel
channel
. After calling this function,QProcess
will no longer receive data on the channel. Any data that has already been received is still available for reading.Call this function to save memory, if you are not interested in the output of the process.
See also
- PySide2.QtCore.QProcess.closeWriteChannel()¶
Schedules the write channel of
QProcess
to be closed. The channel will close once all data has been written to the process. After calling this function, any attempts to write to the process will fail.Closing the write channel is necessary for programs that read input data until the channel has been closed. For example, the program “more” is used to display text data in a console on both Unix and Windows. But it will not display the text data until
QProcess
‘s write channel has been closed. Example:more = QProcess() more.start("more") more.write("Text to display") more.closeWriteChannel() #QProcess will emit readyRead() once "more" starts printing
The write channel is implicitly opened when
start()
is called.See also
- PySide2.QtCore.QProcess.environment()¶
- Return type:
list of strings
Returns the environment that
QProcess
will pass to its child process, or an emptyQStringList
if no environment has been set usingsetEnvironment()
. If no environment has been set, the environment of the calling process will be used.
- PySide2.QtCore.QProcess.error()¶
- Return type:
Returns the type of error that occurred last.
See also
- PySide2.QtCore.QProcess.error(error)
- Parameters:
error –
ProcessError
Note
This function is deprecated.
- PySide2.QtCore.QProcess.errorOccurred(error)¶
- Parameters:
error –
ProcessError
- static PySide2.QtCore.QProcess.execute(command)¶
- Parameters:
command – str
- Return type:
int
Note
This function is deprecated.
This is an overloaded function.
Starts the program
command
in a new process, waits for it to finish, and then returns the exit code.Argument handling is identical to the respective
start()
overload.After the
command
string has been split and unquoted, this function behaves like the overload which takes the arguments as a string list.See also
start()
splitCommand()
- static PySide2.QtCore.QProcess.execute(program, arguments)
- Parameters:
program – str
arguments – list of strings
- Return type:
int
Starts the program
program
with the argumentsarguments
in a new process, waits for it to finish, and then returns the exit code of the process. Any data the new process writes to the console is forwarded to the calling process.The environment and working directory are inherited from the calling process.
Argument handling is identical to the respective
start()
overload.If the process cannot be started, -2 is returned. If the process crashes, -1 is returned. Otherwise, the process’ exit code is returned.
See also
- PySide2.QtCore.QProcess.exitCode()¶
- Return type:
int
Returns the exit code of the last process that finished.
This value is not valid unless
exitStatus()
returnsNormalExit
.
- PySide2.QtCore.QProcess.exitStatus()¶
- Return type:
Returns the exit status of the last process that finished.
On Windows, if the process was terminated with TerminateProcess() from another application, this function will still return
NormalExit
unless the exit code is less than 0.
- PySide2.QtCore.QProcess.finished(exitCode, exitStatus)¶
- Parameters:
exitCode – int
exitStatus –
ExitStatus
- PySide2.QtCore.QProcess.finished(exitCode)
- Parameters:
exitCode – int
Note
This function is deprecated.
- PySide2.QtCore.QProcess.inputChannelMode()¶
- Return type:
Returns the channel mode of the
QProcess
standard input channel.See also
setInputChannelMode()
InputChannelMode
- PySide2.QtCore.QProcess.kill()¶
Kills the current process, causing it to exit immediately.
On Windows, uses TerminateProcess, and on Unix and macOS, the SIGKILL signal is sent to the process.
See also
- static PySide2.QtCore.QProcess.nullDevice()¶
- Return type:
str
The null device of the operating system.
The returned file path uses native directory separators.
- PySide2.QtCore.QProcess.processChannelMode()¶
- Return type:
Returns the channel mode of the
QProcess
standard output and standard error channels.See also
setProcessChannelMode()
ProcessChannelMode
setReadChannel()
- PySide2.QtCore.QProcess.processEnvironment()¶
- Return type:
Returns the environment that
QProcess
will pass to its child process, or an empty object if no environment has been set usingsetEnvironment()
orsetProcessEnvironment()
. If no environment has been set, the environment of the calling process will be used.
- PySide2.QtCore.QProcess.processId()¶
- Return type:
int
Returns the native process identifier for the running process, if available. If no process is currently running,
0
is returned.
- PySide2.QtCore.QProcess.program()¶
- Return type:
str
Returns the program the process was last started with.
See also
- PySide2.QtCore.QProcess.readAllStandardError()¶
- Return type:
Regardless of the current read channel, this function returns all data available from the standard error of the process as a
QByteArray
.See also
readyReadStandardError()
readAllStandardOutput()
readChannel()
setReadChannel()
- PySide2.QtCore.QProcess.readAllStandardOutput()¶
- Return type:
Regardless of the current read channel, this function returns all data available from the standard output of the process as a
QByteArray
.See also
readyReadStandardOutput()
readAllStandardError()
readChannel()
setReadChannel()
- PySide2.QtCore.QProcess.readChannel()¶
- Return type:
Returns the current read channel of the
QProcess
.See also
- PySide2.QtCore.QProcess.setArguments(arguments)¶
- Parameters:
arguments – list of strings
Set the
arguments
to pass to the called program when starting the process. This function must be called beforestart()
.See also
- PySide2.QtCore.QProcess.setEnvironment(environment)¶
- Parameters:
environment – list of strings
Sets the environment that
QProcess
will pass to the child process. The parameterenvironment
is a list of key=value pairs.For example, the following code adds the environment variable
TMPDIR
:QProcess process; QStringList env = QProcess::systemEnvironment(); env << "TMPDIR=C:\\MyApp\\temp"; // Add an environment variable process.setEnvironment(env); process.start("myapp");
Note
This function is less efficient than the
setProcessEnvironment()
function.
- PySide2.QtCore.QProcess.setInputChannelMode(mode)¶
- Parameters:
mode –
InputChannelMode
Sets the channel mode of the
QProcess
standard input channel to themode
specified. This mode will be used the next timestart()
is called.See also
inputChannelMode()
InputChannelMode
- PySide2.QtCore.QProcess.setProcessChannelMode(mode)¶
- Parameters:
mode –
ProcessChannelMode
Sets the channel mode of the
QProcess
standard output and standard error channels to themode
specified. This mode will be used the next timestart()
is called. For example:builder = QProcess() builder.setProcessChannelMode(QProcess.MergedChannels) builder.start("make", ["-j2"]) import sys if not builder.waitForFinished(): sys.stderr.write("Make failed:" + builder.errorString()) else sys.stderr.write("Make output:" + builder.readAll())
See also
processChannelMode()
ProcessChannelMode
setReadChannel()
- PySide2.QtCore.QProcess.setProcessEnvironment(environment)¶
- Parameters:
environment –
PySide2.QtCore.QProcessEnvironment
Sets the
environment
thatQProcess
will pass to the child process.For example, the following code adds the environment variable
TMPDIR
:QProcess process; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable process.setProcessEnvironment(env); process.start("myapp");
Note how, on Windows, environment variable names are case-insensitive.
- PySide2.QtCore.QProcess.setProcessState(state)¶
- Parameters:
state –
ProcessState
Sets the current state of the
QProcess
to thestate
specified.See also
- PySide2.QtCore.QProcess.setProgram(program)¶
- Parameters:
program – str
Set the
program
to use when starting the process. This function must be called beforestart()
.See also
- PySide2.QtCore.QProcess.setReadChannel(channel)¶
- Parameters:
channel –
ProcessChannel
Sets the current read channel of the
QProcess
to the givenchannel
. The current input channel is used by the functionsread()
,readAll()
,readLine()
, andgetChar()
. It also determines which channel triggersQProcess
to emitreadyRead()
.See also
- PySide2.QtCore.QProcess.setStandardErrorFile(fileName[, mode=QIODevice.Truncate])¶
- Parameters:
fileName – str
mode –
OpenMode
Redirects the process’ standard error to the file
fileName
. When the redirection is in place, the standard error read channel is closed: reading from it usingread()
will always fail, as willreadAllStandardError()
. The file will be appended to ifmode
is Append, otherwise, it will be truncated.See
setStandardOutputFile()
for more information on how the file is opened.Note: if
setProcessChannelMode()
was called with an argument ofMergedChannels
, this function has no effect.
- PySide2.QtCore.QProcess.setStandardInputFile(fileName)¶
- Parameters:
fileName – str
Redirects the process’ standard input to the file indicated by
fileName
. When an input redirection is in place, theQProcess
object will be in read-only mode (callingwrite()
will result in error).To make the process read EOF right away, pass
nullDevice()
here. This is cleaner than usingcloseWriteChannel()
before writing any data, because it can be set up prior to starting the process.If the file
fileName
does not exist at the momentstart()
is called or is not readable, starting the process will fail.Calling after the process has started has no effect.
- PySide2.QtCore.QProcess.setStandardOutputFile(fileName[, mode=QIODevice.Truncate])¶
- Parameters:
fileName – str
mode –
OpenMode
Redirects the process’ standard output to the file
fileName
. When the redirection is in place, the standard output read channel is closed: reading from it usingread()
will always fail, as willreadAllStandardOutput()
.To discard all standard output from the process, pass
nullDevice()
here. This is more efficient than simply never reading the standard output, as noQProcess
buffers are filled.If the file
fileName
doesn’t exist at the momentstart()
is called, it will be created. If it cannot be created, the starting will fail.If the file exists and
mode
isTruncate
, the file will be truncated. Otherwise (ifmode
isAppend
), the file will be appended to.Calling after the process has started has no effect.
- PySide2.QtCore.QProcess.setStandardOutputProcess(destination)¶
- Parameters:
destination –
PySide2.QtCore.QProcess
Pipes the standard output stream of this process to the
destination
process’ standard input.The following shell command:
command1 | command2
Can be accomplished with
QProcess
with the following code:process1 = QProcess() process2 = QProcess() process1.setStandardOutputProcess(process2) process1.start("command1") process2.start("command2")
- PySide2.QtCore.QProcess.setWorkingDirectory(dir)¶
- Parameters:
dir – str
Sets the working directory to
dir
.QProcess
will start the process in this directory. The default behavior is to start the process in the working directory of the calling process.Note
On QNX, this may cause all application threads to temporarily freeze.
See also
- PySide2.QtCore.QProcess.setupChildProcess()¶
This function is called in the child process context just before the program is executed on Unix or macOS (i.e., after
fork()
, but beforeexecve()
). Reimplement this function to do last minute initialization of the child process. Example:class SandboxProcess(QProcess): def setupChildProcess(self) # Drop all privileges in the child process, and enter # a chroot jail. os.setgroups(0, 0) os.chroot("/etc/safe") os.chdir("/") os.setgid(safeGid) os.setuid(safeUid) os.umask(0)
You cannot exit the process (by calling exit(), for instance) from this function. If you need to stop the program before it starts execution, your workaround is to emit
finished()
and then call exit().Warning
This function is called by
QProcess
on Unix and macOS only. On Windows and QNX, it is not called.
- PySide2.QtCore.QProcess.start([mode=QIODevice.ReadWrite])¶
- Parameters:
mode –
OpenMode
This is an overloaded function.
Starts the program set by
setProgram()
with arguments set bysetArguments()
. TheOpenMode
is set tomode
.See also
open()
setProgram()
setArguments()
- PySide2.QtCore.QProcess.start(command[, mode=QIODevice.ReadWrite])
- Parameters:
command – str
mode –
OpenMode
Note
This function is deprecated.
This is an overloaded function.
Starts the command
command
in a new process. TheOpenMode
is set tomode
.command
is a single string of text containing both the program name and its arguments. The arguments are separated by one or more spaces. For example:process = QProcess() process.start("del /s *.txt") # same as process.start("del", ["/s", "*.txt"]) ...
Arguments containing spaces must be quoted to be correctly supplied to the new process. For example:
process = QProcess() process.start("dir \"My Documents\"")
Literal quotes in the
command
string are represented by triple quotes. For example:process = QProcess() process.start("dir \"\"\"My Documents\"\"\"")
After the
command
string has been split and unquoted, this function behaves like the overload which takes the arguments as a string list.You can disable this overload by defining
QT_NO_PROCESS_COMBINED_ARGUMENT_START
when you compile your applications. This can be useful if you want to ensure that you are not splitting arguments unintentionally, for example. In virtually all cases, using the other overload is the preferred method.On operating systems where the system API for passing command line arguments to a subprocess natively uses a single string (Windows), one can conceive command lines which cannot be passed via
QProcess
‘s portable list-based API. In these rare cases you need to usesetProgram()
andsetNativeArguments()
instead of this function.See also
splitCommand()
- PySide2.QtCore.QProcess.start(program, arguments[, mode=QIODevice.ReadWrite])
- Parameters:
program – str
arguments – list of strings
mode –
OpenMode
Starts the given
program
in a new process, passing the command line arguments inarguments
.The
QProcess
object will immediately enter the Starting state. If the process starts successfully,QProcess
will emitstarted()
; otherwise,errorOccurred()
will be emitted.Note
Processes are started asynchronously, which means the
started()
anderrorOccurred()
signals may be delayed. CallwaitForStarted()
to make sure the process has started (or has failed to start) and those signals have been emitted.Note
No further splitting of the arguments is performed.
Windows: The arguments are quoted and joined into a command line that is compatible with the
CommandLineToArgvW()
Windows function. For programs that have different command line quoting requirements, you need to usesetNativeArguments()
. One notable program that does not follow theCommandLineToArgvW()
rules is cmd.exe and, by consequence, all batch scripts.The
OpenMode
is set tomode
.If the
QProcess
object is already running a process, a warning may be printed at the console, and the existing process will continue running unaffected.See also
processId()
started()
waitForStarted()
setNativeArguments()
- static PySide2.QtCore.QProcess.startDetached(command)¶
- Parameters:
command – str
- Return type:
bool
Note
This function is deprecated.
- static PySide2.QtCore.QProcess.startDetached(program, arguments)
- Parameters:
program – str
arguments – list of strings
- Return type:
bool
- static PySide2.QtCore.QProcess.startDetached(program, arguments, workingDirectory)
- Parameters:
program – str
arguments – list of strings
workingDirectory – str
- Return type:
(retval, pid)
This function overloads
startDetached()
.Starts the program
program
with the argumentsarguments
in a new process, and detaches from it. Returnstrue
on success; otherwise returnsfalse
. If the calling process exits, the detached process will continue to run unaffected.Argument handling is identical to the respective
start()
overload.The process will be started in the directory
workingDirectory
. IfworkingDirectory
is empty, the working directory is inherited from the calling process.If the function is successful then *``pid`` is set to the process identifier of the started process.
See also
- PySide2.QtCore.QProcess.startDetached([pid=None])
- Parameters:
pid – int
- Return type:
bool
Starts the program set by
setProgram()
with arguments set bysetArguments()
in a new process, and detaches from it. Returnstrue
on success; otherwise returnsfalse
. If the calling process exits, the detached process will continue to run unaffected.Unix: The started process will run in its own session and act like a daemon.
The process will be started in the directory set by
setWorkingDirectory()
. IfworkingDirectory()
is empty, the working directory is inherited from the calling process.Note
On QNX, this may cause all application threads to temporarily freeze.
If the function is successful then *``pid`` is set to the process identifier of the started process. Note that the child process may exit and the PID may become invalid without notice. Furthermore, after the child process exits, the same PID may be recycled and used by a completely different process. User code should be careful when using this variable, especially if one intends to forcibly terminate the process by operating system means.
Only the following property setters are supported by :
setCreateProcessArgumentsModifier()
setNativeArguments()
All other properties of the
QProcess
object are ignored.Note
The called process inherits the console window of the calling process. To suppress console output, redirect standard/error output to
nullDevice()
.See also
start()
startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid)
startDetached(const QString &command)
- PySide2.QtCore.QProcess.state()¶
- Return type:
Returns the current state of the process.
See also
stateChanged()
error()
- static PySide2.QtCore.QProcess.systemEnvironment()¶
- Return type:
list of strings
Returns the environment of the calling process as a list of key=value pairs. Example:
environment = QProcess.systemEnvironment() # environment = [PATH=/usr/bin:/usr/local/bin", # "USER=greg", "HOME=/home/greg"]
This function does not cache the system environment. Therefore, it’s possible to obtain an updated version of the environment if low-level C library functions like
setenv
orputenv
have been called.However, note that repeated calls to this function will recreate the list of environment variables, which is a non-trivial operation.
Note
For new code, it is recommended to use
systemEnvironment()
- PySide2.QtCore.QProcess.terminate()¶
Attempts to terminate the process.
The process may not exit as a result of calling this function (it is given the chance to prompt the user for any unsaved files, etc).
On Windows, posts a WM_CLOSE message to all top-level windows of the process and then to the main thread of the process itself. On Unix and macOS the
SIGTERM
signal is sent.Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling
kill()
.See also
- PySide2.QtCore.QProcess.waitForFinished([msecs=30000])¶
- Parameters:
msecs – int
- Return type:
bool
Blocks until the process has finished and the
finished()
signal has been emitted, or untilmsecs
milliseconds have passed.Returns
true
if the process finished; otherwise returnsfalse
(if the operation timed out, if an error occurred, or if thisQProcess
is already finished).This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
Warning
Calling this function from the main (GUI) thread might cause your user interface to freeze.
If msecs is -1, this function will not time out.
See also
finished()
waitForStarted()
waitForReadyRead()
waitForBytesWritten()
- PySide2.QtCore.QProcess.waitForStarted([msecs=30000])¶
- Parameters:
msecs – int
- Return type:
bool
Blocks until the process has started and the
started()
signal has been emitted, or untilmsecs
milliseconds have passed.Returns
true
if the process was started successfully; otherwise returnsfalse
(if the operation timed out or if an error occurred).This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
Warning
Calling this function from the main (GUI) thread might cause your user interface to freeze.
If msecs is -1, this function will not time out.
Note
On some UNIX operating systems, this function may return true but the process may later report a
FailedToStart
error.See also
started()
waitForReadyRead()
waitForBytesWritten()
waitForFinished()
- PySide2.QtCore.QProcess.workingDirectory()¶
- Return type:
str
If
QProcess
has been assigned a working directory, this function returns the working directory that theQProcess
will enter before the program has started. Otherwise, (i.e., no directory has been assigned,) an empty string is returned, andQProcess
will use the application’s current working directory instead.See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.