That's interesting. But doesn't work.
I tried this:
#include "IdleTimer.hpp"
// Static initialisers
IdleTimer *IdleTimer::m_Instance = 0;
int IdleTimer::m_timeout = 0;
/*!
* Class constructor
*/
IdleTimer
::IdleTimer(QObject *parent,
int seconds
) :{
fParent = parent;
if (seconds)
m_timeout = seconds;
Q_ASSERT(parent);
Q_ASSERT_X(m_timeout, "IdleTimer Constructor", "The timeout must be specified in the first call to instance.");
test_timer->singleShot(10*1000, this, SLOT(createTimer()));
}
void IdleTimer::createTimer()
{
qDebug() << "Creating my timer" << endl;
fParent->installEventFilter(this);
m_timer->singleShot(m_timeout*1000, this, SLOT(idleTimeout()));
}
/*!
* Either reset the timeout to a different value to restart the timer
*/
void IdleTimer::start(int seconds/*=0*/) {
if(seconds)
m_timeout = seconds;
Q_ASSERT_X(m_timeout, "IdleTimer reset", "A timeout must be specified either in this call or in the first call to instance.");
m_timer->start(m_timeout*1000);
}
void IdleTimer::stop() {
m_timer->stop();
}
/*!
* The vent filter
*/
{
if(ev
->type
() == QEvent::KeyPress ||
ev
->type
() == QEvent::MouseMove) {
IdleTimer::m_timer->stop();
return QObject::eventFilter(obj, ev
);
}
// Must return to allow further processing
return QObject::eventFilter(obj, ev
);
}
/*slot*/
void IdleTimer::idleTimeout() {
qDebug("Application has been idle, emitting idle signal ...");
emit idle();
}
#include "IdleTimer.hpp"
// Static initialisers
IdleTimer *IdleTimer::m_Instance = 0;
QTimer *IdleTimer::m_timer = new QTimer();
QTimer *IdleTimer::test_timer = new QTimer();
int IdleTimer::m_timeout = 0;
/*!
* Class constructor
*/
IdleTimer::IdleTimer(QObject *parent, int seconds) :
QObject(parent)
{
fParent = parent;
if (seconds)
m_timeout = seconds;
Q_ASSERT(parent);
Q_ASSERT_X(m_timeout, "IdleTimer Constructor", "The timeout must be specified in the first call to instance.");
test_timer->singleShot(10*1000, this, SLOT(createTimer()));
}
void IdleTimer::createTimer()
{
qDebug() << "Creating my timer" << endl;
fParent->installEventFilter(this);
m_timer->singleShot(m_timeout*1000, this, SLOT(idleTimeout()));
}
/*!
* Either reset the timeout to a different value to restart the timer
*/
void IdleTimer::start(int seconds/*=0*/) {
if(seconds)
m_timeout = seconds;
Q_ASSERT_X(m_timeout, "IdleTimer reset", "A timeout must be specified either in this call or in the first call to instance.");
m_timer->start(m_timeout*1000);
}
void IdleTimer::stop() {
m_timer->stop();
}
/*!
* The vent filter
*/
bool IdleTimer::eventFilter(QObject *obj, QEvent *ev)
{
if(ev->type() == QEvent::KeyPress ||
ev->type() == QEvent::MouseMove)
{
IdleTimer::m_timer->stop();
return QObject::eventFilter(obj, ev);
}
// Must return to allow further processing
return QObject::eventFilter(obj, ev);
}
/*slot*/
void IdleTimer::idleTimeout() {
qDebug("Application has been idle, emitting idle signal ...");
emit idle();
}
To copy to clipboard, switch view to plain text mode
#ifndef IDLETIMER_H
#define IDLETIMER_H
#include <qobject>
#include <qevent>
#include <qtimer>
{
Q_OBJECT
public:
// Singleton class stuff
static IdleTimer
* instance
(QObject *parent
=0,
int seconds
=0) { if (!m_Instance) {
m_Instance = new IdleTimer(parent, seconds);
}
return m_Instance;
}
static void drop() {
qDebug("IdleTimer dropped ...");
m_timer->stop();
if(m_timer) delete m_timer;
if (m_Instance)
delete m_Instance;
m_Instance = 0;
}
void start(int seconds = 0);
void stop();
private:
explicit IdleTimer
(QObject *parent,
int seconds
);
explicit IdleTimer() {}
~IdleTimer() {}
IdleTimer(const IdleTimer &); // hide copy constructor
IdleTimer& operator=(const IdleTimer &); // hide assign op
static IdleTimer *m_Instance;
static int m_timeout;
signals:
void idle();
private slots:
void idleTimeout();
void createTimer();
protected:
};
#endif // IDLETIMER_H
#ifndef IDLETIMER_H
#define IDLETIMER_H
#include <qobject>
#include <qevent>
#include <qtimer>
class IdleTimer : public QObject
{
Q_OBJECT
public:
// Singleton class stuff
static IdleTimer* instance(QObject *parent=0, int seconds=0) {
if (!m_Instance) {
m_Instance = new IdleTimer(parent, seconds);
}
return m_Instance;
}
static void drop() {
qDebug("IdleTimer dropped ...");
m_timer->stop();
if(m_timer) delete m_timer;
if (m_Instance)
delete m_Instance;
m_Instance = 0;
}
void start(int seconds = 0);
void stop();
private:
explicit IdleTimer(QObject *parent, int seconds);
explicit IdleTimer() {}
~IdleTimer() {}
IdleTimer(const IdleTimer &); // hide copy constructor
IdleTimer& operator=(const IdleTimer &); // hide assign op
static IdleTimer *m_Instance;
static QTimer *m_timer;
static QTimer *test_timer;
static int m_timeout;
QObject* fParent;
signals:
void idle();
private slots:
void idleTimeout();
void createTimer();
protected:
bool eventFilter(QObject *obj, QEvent *ev);
};
#endif // IDLETIMER_H
To copy to clipboard, switch view to plain text mode
int main(int argc, char *argv[])
{
Application app(argc, argv);
IdleTimer *timer = IdleTimer::instance(&app,5);
}
int main(int argc, char *argv[])
{
Application app(argc, argv);
IdleTimer *timer = IdleTimer::instance(&app,5);
}
To copy to clipboard, switch view to plain text mode
It prints out the creating timer statement after 10 seconds, but then the timeout message 5 seconds after that, even with clicks and mousemove..
Bookmarks