QT关于屏幕保护程序
来源:互联网 发布:golang udp server 编辑:程序博客网 时间:2024/05/21 22:27
首先,要写一个屏保程序,需要一个定时器来统计多长时间没有处理事件进入休眠,
第二,事件处理是一个全局性的,过滤处理事件,
//直接上源码吧,自己改改。重写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();}
QQ:609162385
阅读全文
0 0
- QT关于屏幕保护程序
- QT关于屏幕保护程序
- QT屏幕保护程序二
- 屏幕保护程序
- c#屏幕保护程序
- 【Qt】关于Qt【转】
- 关于QT
- 关于Qt
- 关于Qt
- 关于Qt
- 关于Qt
- 关于QT
- 关于QT
- 关于Qt
- 关于qt.
- 关于Qt
- 用VB制作屏幕保护程序
- 深入浅出屏幕保护程序编程
- mysql实现物化视图详解及视图与物化视图区别
- Android异步消息处理线程机制
- 【单调栈】codeforces 280b
- ccf集合竞价
- vue.js基础-组件
- QT关于屏幕保护程序
- js循环语句与循环嵌套
- 算法---从一个数组(或者集合中)找出和为某个值的下标
- jinja2.exceptions.UndefinedError: 'form' is undefined
- Uncaught TypeError: window.showModalDialog is not a function chrome 谷歌浏览器
- 图像搜索项目整理笔记
- redis采用序列化方案存对象
- 用户注册与登录功能的实现
- android弱网下优化