日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

[QT]制作软件级屏保

發布時間:2023/12/29 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [QT]制作软件级屏保 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前接手的項目中有一個關于屏保的需求是,如果超過30s未進行操作,軟件自動進入屏保狀態。簡單分析一下:所謂未進行操作即是指未接收到鍵盤或者鼠標事件,超過30s也就是需要用到定時器,屏保狀態就是顯示一張全屏大圖。從QT的角度思考,我們需要創建一個屏保類并且將其做為應用程序的一個事件代理,如果其中有鼠標或者鍵盤事件,則重新刷新定時器,如果超過30s沒有接收到相關事件則全屏顯示屏保窗口。以下就著代碼實例講解一下。

構造一個屏保類CScreenSaver:

cscreensaver.h文件

#ifndef CSCREENSAVER #define CSCREENSAVER#ifndef QOBJECT_H #include <QObject> #endifclass QTimer; class QLabel; class CScreenSaver : public QObject {Q_OBJECTpublic:CScreenSaver(QObject *parent = NULL);~CScreenSaver();protected slots:void slot_timeout();protected://初始化屏保參數void init();//事件接收處理函數,由installEventFilter調用方在接收到事件時調用bool eventFilter(QObject *watched, QEvent *event);private://定時器QTimer *timer;//用于顯示屏保圖片的對象QLabel *label;//初始屏保等待超時時間static const unsigned WAIT_TIME = 30000; };#endif cscreensaver.cpp文件

#include <cscreensaver.h>#include <QFile> #include <QLabel> #include <QEvent> #include <QTimer> #include <QPixmap> #include <QSettings>CScreenSaver::CScreenSaver(QObject *parent): QObject(parent), waitInterval(WAIT_TIME) {init(); }CScreenSaver::~CScreenSaver() { }void CScreenSaver::init() {unsigned waitInterval;QString urlPath;//讀取屏保配置QSettings settings(QApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);settings.beginGroup("SCREENSAVER");if (settings.contains("Interval")){bool ok;waitInterval = settings.value("Interval").toUInt(&ok);if (!ok)waitInterval = WAIT_TIME;}if (settings.contains("PicPath")){urlPath = settings.value("PicPath").toString();}settings.endGroup();//設置并啟動timer。如果超過30s,則一直顯示屏保并不再觸發直到再次刷新定時器timer = new QTimer;timer->setSingleShot(true);connect(timer, SIGNAL(timeout()), this, SLOT(slot_timeout()));timer->start(waitInterval);//屏保窗口QRect screenRect = QApplication::?desktop()->screenGeometry(0);label = new QLabel();label->setGeometry(screenRect);label->setWindowFlags(Qt::FramelessWindowHint);//拉伸背景圖片label->setScaledContents(true);QPixmap pmp;pmp.load(urlPath);label->setPixmap(pmp);label->hide(); }bool CScreenSaver::eventFilter(QObject *obj, QEvent *event) {//判斷事件類型if (event->type() == QEvent::KeyPress || event->type() == QEvent::MouseMove|| event->type() == QEvent::MouseButtonPress) {//有鼠標或鍵盤事件則重置timertimer->start();label->hide();}return QObject::eventFilter(obj, event); }void CScreenSaver::slot_timeout() {//顯示屏保label->activateWindow();label->show(); }
main.cpp文件中

#include <QApplication>... #include <cscreensaver.h>int main(int argc, char *argv[]) {QApplication app(argc, argv);...CScreenSaver screenSaver;//全局接收并委托處理事件app.installEventFilter(&screenSaver);...return app.exec(); }

歡迎訪問cuzn小站

總結

以上是生活随笔為你收集整理的[QT]制作软件级屏保的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。