Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_io_qprocess.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QProcess>
5#include <QDebug>
6#include <QtCore/qglobal.h>
7
9{
10
11 //! [0]
12 QProcess builder;
13 builder.setProcessChannelMode(QProcess::MergedChannels);
14 builder.start("make", QStringList() << "-j2");
15
16 if (!builder.waitForFinished())
17 qDebug() << "Make failed:" << builder.errorString();
18 else
19 qDebug() << "Make output:" << builder.readAll();
20 //! [0]
21
22
23 //! [1]
24 QProcess more;
25 more.start("more");
26 more.write("Text to display");
27 more.closeWriteChannel();
28 // QProcess will emit readyRead() once "more" starts printing
29 //! [1]
30
31
32# if 0
33 //! [2]
34 command1 | command2
35 //! [2]
36#endif
37
38
39 //! [3]
40 QProcess process1;
41 QProcess process2;
42
43 process1.setStandardOutputProcess(&process2);
44
45 process1.start("command1");
46 process2.start("command2");
47 //! [3]
48}
49
50
51#if defined(Q_OS_UNIX)
52
53#include <unistd.h>
54#include <grp.h>
55#include <sys/stat.h>
56#include <sys/types.h>
57
58gid_t safeGid;
59uid_t safeUid;
60
61//! [4]
62void runSandboxed(const QString &name, const QStringList &arguments)
63{
64 QProcess proc;
65 proc.setChildProcessModifier([] {
66 // Drop all privileges in the child process, and enter
67 // a chroot jail.
68 ::setgroups(0, nullptr);
69 ::chroot("/run/safedir");
70 ::chdir("/");
71 ::setgid(safeGid);
72 ::setuid(safeUid);
73 ::umask(077);
74 });
75 proc.start(name, arguments);
76 proc.waitForFinished();
77}
78//! [4]
79#endif
80
82{
83 {
84 //! [5]
85 QProcess process;
86 process.startCommand("del /s *.txt");
87 // same as process.start("del", QStringList() << "/s" << "*.txt");
88 //...
89 //! [5]
90 }
91
92 {
93 //! [6]
94 QProcess process;
95 process.startCommand("dir \"My Documents\"");
96 //! [6]
97 }
98
99 {
100 //! [7]
101 QProcess process;
102 process.startCommand("dir \"Epic 12\"\"\" Singles\"");
103 //! [7]
104 }
105
106 {
107 //! [8]
108 QStringList environment = QProcess::systemEnvironment();
109 // environment = {"PATH=/usr/bin:/usr/local/bin",
110 // "USER=greg", "HOME=/home/greg"}
111 //! [8]
112 }
113}
bool examples()
[3]
void wrapInFunction()
[16]