QtCore/qprocess.h Source File

qprocess.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QPROCESS_H
43 #define QPROCESS_H
44 
45 #include <QtCore/qiodevice.h>
46 #include <QtCore/qstringlist.h>
47 #include <QtCore/qshareddata.h>
48 
50 
52 
53 QT_MODULE(Core)
54 
55 #ifndef QT_NO_PROCESS
56 
57 #if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc)
58 typedef qint64 Q_PID;
59 #else
61 typedef struct _PROCESS_INFORMATION *Q_PID;
63 #endif
64 
65 class QProcessPrivate;
66 class QProcessEnvironmentPrivate;
67 
68 class Q_CORE_EXPORT QProcessEnvironment
69 {
70 public:
74  QProcessEnvironment &operator=(const QProcessEnvironment &other);
75 
76  bool operator==(const QProcessEnvironment &other) const;
77  inline bool operator!=(const QProcessEnvironment &other) const
78  { return !(*this == other); }
79 
80  bool isEmpty() const;
81  void clear();
82 
83  bool contains(const QString &name) const;
84  void insert(const QString &name, const QString &value);
85  void remove(const QString &name);
86  QString value(const QString &name, const QString &defaultValue = QString()) const;
87 
88  QStringList toStringList() const;
89 
90  QStringList keys() const;
91 
92  void insert(const QProcessEnvironment &e);
93 
94  static QProcessEnvironment systemEnvironment();
95 
96 private:
97  friend class QProcessPrivate;
98  friend class QProcessEnvironmentPrivate;
100 };
101 
102 class Q_CORE_EXPORT QProcess : public QIODevice
103 {
104  Q_OBJECT
105 public:
107  FailedToStart, //### file not found, resource error
112  UnknownError
113  };
117  Running
118  };
121  StandardError
122  };
126  ForwardedChannels
127  };
128  enum ExitStatus {
130  CrashExit
131  };
132 
133  explicit QProcess(QObject *parent = 0);
134  virtual ~QProcess();
135 
136  void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite);
137  void start(const QString &program, OpenMode mode = ReadWrite);
138 
139  ProcessChannelMode readChannelMode() const;
140  void setReadChannelMode(ProcessChannelMode mode);
141  ProcessChannelMode processChannelMode() const;
142  void setProcessChannelMode(ProcessChannelMode mode);
143 
144  ProcessChannel readChannel() const;
145  void setReadChannel(ProcessChannel channel);
146 
147  void closeReadChannel(ProcessChannel channel);
148  void closeWriteChannel();
149 
150  void setStandardInputFile(const QString &fileName);
151  void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
152  void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
153  void setStandardOutputProcess(QProcess *destination);
154 
155 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
156  QString nativeArguments() const;
157  void setNativeArguments(const QString &arguments);
158 #endif
159 
160  QString workingDirectory() const;
161  void setWorkingDirectory(const QString &dir);
162 
163  void setEnvironment(const QStringList &environment);
164  QStringList environment() const;
165  void setProcessEnvironment(const QProcessEnvironment &environment);
166  QProcessEnvironment processEnvironment() const;
167 
168  QProcess::ProcessError error() const;
169  QProcess::ProcessState state() const;
170 
171  // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
172  Q_PID pid() const;
173 
174  bool waitForStarted(int msecs = 30000);
175  bool waitForReadyRead(int msecs = 30000);
176  bool waitForBytesWritten(int msecs = 30000);
177  bool waitForFinished(int msecs = 30000);
178 
179  QByteArray readAllStandardOutput();
180  QByteArray readAllStandardError();
181 
182  int exitCode() const;
183  QProcess::ExitStatus exitStatus() const;
184 
185  // QIODevice
186  qint64 bytesAvailable() const;
187  qint64 bytesToWrite() const;
188  bool isSequential() const;
189  bool canReadLine() const;
190  void close();
191  bool atEnd() const;
192 
193  static int execute(const QString &program, const QStringList &arguments);
194  static int execute(const QString &program);
195 
196  static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory,
197  qint64 *pid = 0);
198  static bool startDetached(const QString &program, const QStringList &arguments);
199  static bool startDetached(const QString &program);
200 
201  static QStringList systemEnvironment();
202 
203 public Q_SLOTS:
204  void terminate();
205  void kill();
206 
207 Q_SIGNALS:
208  void started();
209  void finished(int exitCode);
210  void finished(int exitCode, QProcess::ExitStatus exitStatus);
211  void error(QProcess::ProcessError error);
212  void stateChanged(QProcess::ProcessState state);
213 
214  void readyReadStandardOutput();
215  void readyReadStandardError();
216 
217 protected:
218  void setProcessState(ProcessState state);
219 
220  virtual void setupChildProcess();
221 
222  // QIODevice
223  qint64 readData(char *data, qint64 maxlen);
224  qint64 writeData(const char *data, qint64 len);
225 
226 private:
227  Q_DECLARE_PRIVATE(QProcess)
228  Q_DISABLE_COPY(QProcess)
229 
230  Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
231  Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
232  Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
233  Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
234  Q_PRIVATE_SLOT(d_func(), bool _q_processDied())
235  Q_PRIVATE_SLOT(d_func(), void _q_notified())
236  friend class QProcessManager;
237 };
238 
239 #endif // QT_NO_PROCESS
240 
242 
244 
245 #endif // QPROCESS_H
bool operator!=(const QProcessEnvironment &other) const
Definition: qprocess.h:77
GLint mode
Definition: GLee.h:4479
#define QT_END_NAMESPACE
Definition: qglobal.h:128
GLuint start
Definition: GLee.h:872
ProcessChannel
Definition: qprocess.h:119
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
virtual bool isSequential() const
long long qint64
Definition: qglobal.h:947
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
virtual qint64 writeData(const char *data, qint64 len)=0
#define Q_SLOTS
Definition: qobjectdefs.h:71
ProcessState
Definition: qprocess.h:114
#define Q_SIGNALS
Definition: qobjectdefs.h:72
virtual qint64 bytesToWrite() const
virtual qint64 readData(char *data, qint64 maxlen)=0
bool operator==(const Attribute &cA, const AttributeInstance< type > &cB)
This operator compares the two attributes and NOT their values.
Definition: node.h:577
virtual bool canReadLine() const
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: GLee.h:880
GLuint program
Definition: GLee.h:2696
virtual bool waitForReadyRead(int msecs)
QT_BEGIN_HEADER QT_BEGIN_NAMESPACE typedef qint64 Q_PID
Definition: qprocess.h:58
ProcessError
Definition: qprocess.h:106
#define Q_OBJECT
Definition: qobjectdefs.h:157
GLsizei const GLfloat * value
Definition: GLee.h:1742
int int channel
Definition: GLee.h:10533
GLuint const GLchar * name
Definition: GLee.h:1704
virtual bool waitForBytesWritten(int msecs)
virtual qint64 bytesAvailable() const
virtual bool atEnd() const
GLenum GLsizei len
Definition: GLee.h:2695
#define QT_END_HEADER
Definition: qglobal.h:142
virtual void close()
ProcessChannelMode
Definition: qprocess.h:123