summaryrefslogtreecommitdiffstats
path: root/src/processmanager/doc/src/standardpm.qdoc
blob: 38578626657a791c4b9f6b98fc509bfa80e3edce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of ProcessManager
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

/*!

\page standardpm.html
\previouspage Backend Process Manager
\contentspage {Standard Process Manager} {Contents}
\nextpage Declarative Process Manager

\title Standard Process Manager

The standard QProcessManager object wraps the QProcessBackendManager
object, creates QProcessFrontend objects to wrap each QProcessBackend
object, and assigns unique string identifiers to each process.
This approach provides several advantages.  First, the QProcessManager can
maintain a list of running processes indexed by identifier.  Second, the
QProcessFrontend objects can be subclassed.  This provides a convenient
way of adding convenience functions to the QProcessFrontend objects. Third,
instead of connecting to signals from each process, one can subclass
the QProcessManager itself and override the handler functions.

The unique identifier assigned to each process created takes the form
of "NAME-NUMBER", where NAME comes from the QProcessInfo.name attribute
and NUMBER is a unique integer assigned at process creation.

\section2 Directly using the QProcessManager

You can directly use the QProcessManager in your code.
\code
void MyClass::setup()
{
  QProcessManager *manager = new QProcessManager;
  manager->addFactory(new QUnixProcessBackendFactory);
  m_manager = manager;
}

void MyClass::start(QProcessInfo info)
{
  QProcessFrontend *frontend = m_manager->start(info);
  connect(frontend, SIGNAL(started()), SLOT(started()));
  connect(frontend, SIGNAL(finished(int, QProcess::ExitStatus)),
                   SLOT(finished(int, QProcess::ExitStatus)));
}

void MyClass::started()
{
  QProcessFrontend *frontend = qobject_cast<QProcessFrontend *>(sender());
  if (frontend)
    qDebug() << "Process" << identifier << "started with pid=" << frontend->pid();
}

void MyClass::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
  QProcessFrontend *frontend = qobject_cast<QProcessFrontend *>(sender());
  if (frontend)
    qDebug() << "Process" << identifier << "stopped with" << exitCode << exitStatus;
}
\endcode


\section2 Subclassing QProcessManager

You can subclass the process manager to gain more control.  This
avoids connecting signals to every process you create.

\code
class Example : public QProcessManager
{
  Q_OBJECT
public:
  Example(QObject *parent=0);

protected slots:
  void processFrontendStarted();
  void processFrontendFinished(int, QProcess::ExitStatus);
};

void Example::Example(QObject *parent)
 : QProcessManager(parent)
{
  addFactory(new UnixProcessBackendFactory);
}

void Example::processFrontendStarted()
{
  QProcessManager::processFrontendStarted();
  QProcessFrontend *frontend = qobject_cast<QProcessFrontend *>(sender());
  qDebug() << "Process" << frontend->identifier() << "has started";
}

void Example::processFrontendFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
  QProcessManager::processFrontendFinished(exitCode, exitStatus);
  QProcessFrontend *frontend = qobject_cast<QProcessFrontend *>(sender());
  qDebug() << "Process" << frontend->identifier() << "has finished" << exitCode << exitStatus;
}

\endcode

\section2 Subclass QProcessFrontend objects

To subclass QProcessFrontend objects, override the \l
{QProcessManager::} {createFrontend()} function and return an object
that derived from QProcessFrontend.  For example:

\code

class MyFrontend : public QProcessFrontend {
  Q_OBJECT
public:
  MyFrontend(QProcessBackend *backend) : QProcessFrontend(backend) {}
protected:
  void handleStateChanged(QProcess::ProcessState);
signals:
  void stopped();
};

void MyFrontend::handleStateChanged(QProcess::ProcessState state)
{
  if (state == QProcess::NotRunning)
    emit stopped();
}

class MyManager : public QProcessManager
{
  Q_OBJECT
public:
  MyManager(QObject *parent=0) : QProcessManager(parent) {}
protected :
  QProcessFrontend *createFrontend(QProcessBackend *backend) {
    return new MyFrontend(backend);
  }
};

\endcode

In the above example, the custom \c MyFrontend class raises a new \c
stopped() signal when the child process stops running.  The \c
MyManager class reimplements \l {QProcessManager::} {createFrontend()}
to return a \c MyFrontend objects for each created process.

*/