Qt 4.8
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;
67 
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;
100 };
101 
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 
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);
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:
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())
237 };
238 
239 #endif // QT_NO_PROCESS
240 
242 
244 
245 #endif // QPROCESS_H
The QProcessEnvironment class holds the environment variables that can be passed to a program...
Definition: qprocess.h:68
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
ProcessError
This enum describes the different types of errors that are reported by QProcess.
Definition: qprocess.h:106
#define QT_MODULE(x)
Definition: qglobal.h:2783
virtual bool waitForReadyRead(int msecs)
Blocks until new data is available for reading and the readyRead() signal has been emitted...
Definition: qiodevice.cpp:1616
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition: qiodevice.cpp:590
ProcessChannel
This enum describes the process channels used by the running process.
Definition: qprocess.h:119
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define error(msg)
virtual qint64 writeData(const char *data, qint64 len)=0
Writes up to maxSize bytes from data to the device.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
ProcessState
This enum describes the different states of QProcess.
Definition: qprocess.h:114
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
#define Q_SLOTS
Definition: qobjectdefs.h:71
QSharedDataPointer< QProcessEnvironmentPrivate > d
Definition: qprocess.h:99
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711
QStringList keys
virtual qint64 bytesToWrite() const
For buffered devices, this function returns the number of bytes waiting to be written.
Definition: qiodevice.cpp:767
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool isEmpty(const char *str)
ProcessChannelMode
This enum describes the process channel modes of QProcess.
Definition: qprocess.h:123
const char * name
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static const char * data(const QByteArray &arr)
__int64 qint64
Definition: qglobal.h:942
#define Q_OBJECT
Definition: qobjectdefs.h:157
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
virtual qint64 readData(char *data, qint64 maxlen)=0
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
bool operator!=(const QProcessEnvironment &other) const
Returns true if this and the other QProcessEnvironment objects are different.
Definition: qprocess.h:77
ExitStatus
This enum describes the different exit statuses of QProcess.
Definition: qprocess.h:128
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
Definition: qiodevice.cpp:752
virtual bool waitForBytesWritten(int msecs)
For buffered devices, this function waits until a payload of buffered written data has been written t...
Definition: qiodevice.cpp:1648
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
Definition: qiodevice.cpp:1330
#define class
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define QT_END_HEADER
Definition: qglobal.h:137
static QString fileName(const QString &fileUrl)
bool operator==(QBool b1, bool b2)
Definition: qglobal.h:2023
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102