diff options
author | Jesper K. Pedersen <jesper.pedersen@kdab.com> | 2013-04-18 07:20:25 +0200 |
---|---|---|
committer | Jesper K. Pedersen <jesper.pedersen@kdab.com> | 2013-04-18 10:42:29 +0200 |
commit | fb804983938054e5177bfce69a95275bda305b37 (patch) | |
tree | 184d563db74b3ed7b4a3e09bee211f367e67b752 /utils | |
parent | 887194939dc7443fa8df1f5ac0eedf118b2c6b88 (diff) |
introduced a utils directory
Change-Id: I48f02a6a80570e3305cbad02120414f1bbe48d59
Reviewed-by: Nicolas Arnaud-Cormos <nicolas@kdab.com>
Diffstat (limited to 'utils')
-rw-r--r-- | utils/signalwaiter.cpp | 31 | ||||
-rw-r--r-- | utils/signalwaiter.h | 31 |
2 files changed, 62 insertions, 0 deletions
diff --git a/utils/signalwaiter.cpp b/utils/signalwaiter.cpp new file mode 100644 index 0000000..3550e5b --- /dev/null +++ b/utils/signalwaiter.cpp @@ -0,0 +1,31 @@ +#include "signalwaiter.h" + +#include <QElapsedTimer> +#include <QCoreApplication> + +namespace Scripting { +namespace Internal { + +SignalWaiter::SignalWaiter(QObject *parent) : + QObject(parent), m_received(false) +{ +} + +bool SignalWaiter::wait(QObject *object, const char *signal, int msecs) +{ + connect( object, signal, this, SLOT(slotReceived())); + QElapsedTimer timer; + timer.start(); + while ( !m_received && timer.elapsed() < msecs ) { + qApp->processEvents(QEventLoop::ExcludeUserInputEvents, 100); + } + return m_received; +} + +void SignalWaiter::slotReceived() +{ + m_received = true; +} + +} // namespace Internal +} // namespace Scripting diff --git a/utils/signalwaiter.h b/utils/signalwaiter.h new file mode 100644 index 0000000..a413072 --- /dev/null +++ b/utils/signalwaiter.h @@ -0,0 +1,31 @@ +#ifndef SCRIPTING_INTERNAL_SIGNALWAITER_H +#define SCRIPTING_INTERNAL_SIGNALWAITER_H + +#include <QObject> + +namespace Scripting { +namespace Internal { + +/** + * @brief Wait for a signal to emit + * + * The method wait will block until the provided signal is emitted or the time out is reached. While blocking it will run the event loop. + */ +class SignalWaiter : public QObject +{ + Q_OBJECT +public: + explicit SignalWaiter(QObject *parent = 0); + bool wait(QObject* object, const char* signal, int msecs); + +private slots: + void slotReceived(); + +private: + bool m_received; +}; + +} // namespace Internal +} // namespace Scripting + +#endif // SCRIPTING_INTERNAL_SIGNALWAITER_H |