Add function to get the actual PID from QProcess

It was not possible to get the actual process ID (in a cross-platform
manner) from QProcess, as the user would need to handle the returned
typedef (Q_PID) differently on Unix and Windows.

On Unix Q_PID is the actual process ID, but on Windows it's a pointer
to a PROCESS_INFORMATION structure, which among other fields contains
the process ID. Instead of returning a pointer on Windows,
QProcess::processId() will return the actual process ID on both Windows
and Unix.

[ChangeLog][QtCore][QProcess] Added processId() to QProcess. This
function will, unlike pid(), return the actual process identifier on
both Window and Unix.

Task-number: QTBUG-26136
Change-Id: I853ab721297e2dd9cda006666144179a9e25b73d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Christian Strømme 2014-01-21 14:54:27 +01:00 committed by The Qt Project
parent 9dc7c92dcb
commit 2223cc8a2d
3 changed files with 30 additions and 3 deletions

View File

@ -1545,16 +1545,40 @@ void QProcess::setWorkingDirectory(const QString &dir)
d->workingDirectory = dir; d->workingDirectory = dir;
} }
/*! /*!
\deprecated
Use processId() instead.
Returns the native process identifier for the running process, if Returns the native process identifier for the running process, if
available. If no process is currently running, 0 is returned. available. If no process is currently running, \c 0 is returned.
\note Unlike \l processId(), pid() returns an integer on Unix and a pointer on Windows.
\sa Q_PID, processId()
*/ */
Q_PID QProcess::pid() const Q_PID QProcess::pid() const // ### Qt 6 remove or rename this method to processInformation()
{ {
Q_D(const QProcess); Q_D(const QProcess);
return d->pid; return d->pid;
} }
/*!
\since 5.3
Returns the native process identifier for the running process, if
available. If no process is currently running, \c 0 is returned.
*/
qint64 QProcess::processId() const
{
Q_D(const QProcess);
#ifdef Q_OS_WIN
return d->pid ? d->pid->dwProcessId : 0;
#else
return d->pid;
#endif
}
/*! \reimp /*! \reimp
This function operates on the current read channel. This function operates on the current read channel.

View File

@ -187,6 +187,7 @@ public:
// #### Qt 5: Q_PID is a pointer on Windows and a value on Unix // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
Q_PID pid() const; Q_PID pid() const;
qint64 processId() const;
bool waitForStarted(int msecs = 30000); bool waitForStarted(int msecs = 30000);
bool waitForReadyRead(int msecs = 30000); bool waitForReadyRead(int msecs = 30000);

View File

@ -943,7 +943,7 @@ void tst_QProcess::hardExit()
void tst_QProcess::softExit() void tst_QProcess::softExit()
{ {
QProcess proc; QProcess proc;
QCOMPARE(proc.processId(), 0);
proc.start("testSoftExit/testSoftExit"); proc.start("testSoftExit/testSoftExit");
QVERIFY(proc.waitForStarted(10000)); QVERIFY(proc.waitForStarted(10000));
@ -951,6 +951,8 @@ void tst_QProcess::softExit()
QVERIFY(proc.waitForReadyRead(10000)); QVERIFY(proc.waitForReadyRead(10000));
#endif #endif
QVERIFY(proc.processId() > 0);
proc.terminate(); proc.terminate();
QVERIFY(proc.waitForFinished(10000)); QVERIFY(proc.waitForFinished(10000));