#include <QtGui>
#include <QDebug>
Q_OBJECT
static const int timeoutPeriod = 3000;
public:
// Set up the timer
m_timer.setInterval( timeoutPeriod );
m_timer.setSingleShot( true );
// These connections do the work
connect(&m_proc, SIGNAL(started()), &m_timer, SLOT(start()));
connect(&m_timer, SIGNAL(timeout()), this, SLOT(timedOut()));
// these are here only to illustrate what is being called
connect(&m_proc, SIGNAL(started()), SLOT(processStarted()));
connect(&m_proc,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
SLOT(processFinished
(int,
QProcess::ExitStatus)));
}
void launch() {
static const QString cmd
= "/bin/ls";
// Start the process
m_proc.start(cmd, args);
}
public slots:
void timedOut() {
qDebug() << "Timed out";
if (m_proc.
state() != QProcess::NotRunning) { qDebug() << "Terminating";
m_proc.terminate();
}
}
// these are here only to illustrate what is being called
void processStarted() { qDebug() << "Process started"; }
void processFinished
(int exitCode,
QProcess::ExitStatus exitStatus
) { qDebug
() <<
"Process Finished" << exitCode << exitStatus;
} };
int main(int argc, char **argv)
{
Launcher launcher;
launcher.launch();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QDebug>
class Launcher: public QObject {
Q_OBJECT
static const int timeoutPeriod = 3000;
QTimer m_timer;
QProcess m_proc;
public:
Launcher(QObject *p = 0): QObject(p) {
// Set up the timer
m_timer.setInterval( timeoutPeriod );
m_timer.setSingleShot( true );
// These connections do the work
connect(&m_proc, SIGNAL(started()), &m_timer, SLOT(start()));
connect(&m_timer, SIGNAL(timeout()), this, SLOT(timedOut()));
// these are here only to illustrate what is being called
connect(&m_proc, SIGNAL(started()), SLOT(processStarted()));
connect(&m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(processFinished(int,QProcess::ExitStatus)));
}
void launch() {
static const QString cmd = "/bin/ls";
static const QStringList args = QStringList() << "-lR" << "/";
// Start the process
m_proc.start(cmd, args);
}
public slots:
void timedOut() {
qDebug() << "Timed out";
if (m_proc.state() != QProcess::NotRunning) {
qDebug() << "Terminating";
m_proc.terminate();
}
}
// these are here only to illustrate what is being called
void processStarted() { qDebug() << "Process started"; }
void processFinished(int exitCode, QProcess::ExitStatus exitStatus) { qDebug() << "Process Finished" << exitCode << exitStatus; }
};
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Launcher launcher;
launcher.launch();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
You could improve it by stopping the timer when the process finishes.
Bookmarks