QT关于屏幕保护程序

首先,要写一个屏保程序,需要一个定时器来统计多长时间没有处理事件进入休眠,

第二,事件处理是一个全局性的,过滤处理事件,

//直接上源码吧,自己改改。重写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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值