首先,要写一个屏保程序,需要一个定时器来统计多长时间没有处理事件进入休眠,
第二,事件处理是一个全局性的,过滤处理事件,
//直接上源码吧,自己改改。重写Application
#ifndef GLOBALAPPLICATION_H #define GLOBALAPPLICATION_H #include <QApplication> #include <QTimer> #include <QWidget> #include <QSplashScreen> class GlobalApplicationPri : public QWidget { Q_OBJECT Q_PROPERTY(bool mState READ state WRITE setState NOTIFY stateChanged) public: explicit GlobalApplicationPri(QWidget *parent = 0); ~GlobalApplicationPri(); void setWindowInstance(QWidget*wnd,QSplashScreen *); bool state(); void setState(bool);//周围状态 public slots: void mAcceptGlobalEvent(); void mAcceptActivationEvent(); signals: void stateChanged(); private: QWidget *mwidget; QTimer *mtime; QSplashScreen *msplash; bool mState; }; class GlobalApplication : public QApplication{ public: GlobalApplication(int &argc,char **argv,QWidget *parent = 0); ~GlobalApplication(); bool notify(QObject*, QEvent *); void setWindowInstance(QWidget*wnd,QSplashScreen *); private: GlobalApplicationPri *m_obj; QWidget *mwidget; QSplashScreen *msplash; }; #endif // GLOBALAPPLICATION_H
下面是.cpp文件
#include "globalapplication.h"
#include <QKeyEvent>
#include <QDebug>
GlobalApplication::GlobalApplication(int &argc,char **argv,QWidget *parent):
QApplication(argc,argv)
{
m_obj = new GlobalApplicationPri();
}
GlobalApplication::~GlobalApplication()
{
delete m_obj;
}
bool GlobalApplication::notify(QObject *obj, QEvent *e)
{
const QMetaObject* objMeta = obj->metaObject();
QString clName = objMeta->className();
if(e->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
if(keyEvent->key() == Qt::Key_F1)
{
qDebug()<< clName;
qDebug() << "F1";
}
}
else if(e->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if(mouseEvent->buttons() == Qt::LeftButton){
qDebug() << "left";
m_obj->setState(true);
}
if(mouseEvent->buttons() == Qt::RightButton){
qDebug() << "Right";
}
}
return QApplication::notify(obj,e);
}
void GlobalApplication::setWindowInstance(QWidget *wnd,QSplashScreen *splash)
{
mwidget = wnd;
msplash = splash;
m_obj->setWindowInstance(wnd,splash);
}
GlobalApplicationPri::GlobalApplicationPri(QWidget *parent)
{
mwidget = Q_NULLPTR;
mtime = Q_NULLPTR;
msplash = Q_NULLPTR;
mtime = new QTimer(this);
connect(mtime, SIGNAL(timeout()),this, SLOT(mAcceptGlobalEvent()));
mtime->start(20000);
connect(this, SIGNAL(stateChanged()),this, SLOT(mAcceptActivationEvent()));
}
GlobalApplicationPri::~GlobalApplicationPri()
{
}
void GlobalApplicationPri::setWindowInstance(QWidget *wnd, QSplashScreen *splash)
{
mwidget = wnd;
msplash = splash;
}
bool GlobalApplicationPri::state()
{
return mState;
}
void GlobalApplicationPri::setState(bool mcont)
{
mState = mcont;
emit stateChanged();
}
void GlobalApplicationPri::mAcceptGlobalEvent()
{
mwidget->hide();
msplash->show();
}
void GlobalApplicationPri::mAcceptActivationEvent()
{
mtime->stop();
msplash->hide();
mwidget->show();
mtime->start(20000);
}
最后来看看主函数
#include "mainwindow.h"
#include <QApplication>
using namespace LENSOMETER;
#include <QObject>
#include <QApplication>
#include <QFontDatabase>
#include <QSplashScreen>
#include "../../common/global/globalapplication.h"
#include "mainwindow.h"
#include <QThread>
int main(int argc, char *argv[])
{
GlobalApplication app(argc, argv);
//
// /// \brief splash
// ///
MainWindow mainWin;
QSplashScreen *splash = new QSplashScreen;
app.setWindowInstance(&mainWin,splash);
splash->setPixmap(QPixmap(":/ui_image/load.png"));
splash->show();//显示图片
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(QObject::tr("Setting up the main window..."),topRight, Qt::white);
splash->showMessage(QObject::tr("Loading modules..."),
topRight, Qt::white);
splash->showMessage(QObject::tr("Establishing connections..."),
topRight, Qt::white);
QThread::sleep(1);
mainWin.show();
splash->finish(&mainWin);
return app.exec();
}