Thanks, so I went with my original idea with jacek's suggestion of using the qapp rather than the main window, but the solution should be generic. Here is an example for anyone that might want to use a similar code.
Header
#include <QtGui>
#include <QtCore>
class activityFilter
: public QObject{
Q_OBJECT
Q_PROPERTY(int interval READ interval WRITE setInterval)
public:
activityFilter
(QObject *parent
= 0);
int interval () const { return _interval; };
void setInterval ( int msec ) { _interval = msec; };
signals:
void userInactive();
public slots:
void timeout();
protected:
private:
int _interval;
};
#include <QtGui>
#include <QtCore>
class activityFilter : public QObject
{
Q_OBJECT
Q_PROPERTY(int interval READ interval WRITE setInterval)
public:
activityFilter(QObject *parent = 0);
int interval () const { return _interval; };
void setInterval ( int msec ) { _interval = msec; };
signals:
void userInactive();
public slots:
void timeout();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
QTimer *timer;
int _interval;
};
To copy to clipboard, switch view to plain text mode
Implementation
#include <QtGui>
#include <QtCore>
#include "af.h"
activityFilter
::activityFilter(QObject *parent
) : QObject(parent
), _interval
(3000){
timer->start(_interval);
connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
{
if (event
->type
() != QEvent::Timer) // this is needed or else the timer is reset by its own timeout signal event before the signal is sent timer->start(_interval);
return QObject::eventFilter(obj, event
);
// just a wrapper, we don't want to override any events }
void activityFilter::timeout()
{
qDebug() << "user timeout";
emit userInactive();
}
#include <QtGui>
#include <QtCore>
#include "af.h"
activityFilter::activityFilter(QObject *parent) : QObject(parent), _interval(3000)
{
timer = new QTimer(this);
timer->start(_interval);
connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
bool activityFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() != QEvent::Timer) // this is needed or else the timer is reset by its own timeout signal event before the signal is sent
timer->start(_interval);
return QObject::eventFilter(obj, event); // just a wrapper, we don't want to override any events
}
void activityFilter::timeout()
{
qDebug() << "user timeout";
emit userInactive();
}
To copy to clipboard, switch view to plain text mode
Example
#include <QtGui>
#include <QtCore>
#include "ui_main.h"
#include "af.h"
int main(int argc, char * argv[])
{
app.setQuitOnLastWindowClosed(true);
activityFilter *ef;
ef = new activityFilter(&app);
app.installEventFilter(ef);
Ui_dialog ui;
ui.setupUi(&win);
QObject::connect(ef,
SIGNAL( userInactive
() ),
&win, SLOT( close() ));
win.show();
return app.exec();
}
#include <QtGui>
#include <QtCore>
#include "ui_main.h"
#include "af.h"
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(true);
activityFilter *ef;
ef = new activityFilter(&app);
app.installEventFilter(ef);
QDialog win;
Ui_dialog ui;
ui.setupUi(&win);
QObject::connect(ef, SIGNAL( userInactive() ),
&win, SLOT( close() ));
win.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I created a quick dialog in designer saved as main.ui to make this work.
Bookmarks