[QT] Making software-level Screensavers

Source: Internet
Author: User

Welcome to visit CuZn Station

One of the previous projects that had been taken on was the need to have a screensaver that would automatically go into the screensaver if it was not operating more than 30s. Simple analysis: The so-called non-operation refers to the non-receipt of keyboard or mouse events, more than 30s is required to use the timer, screen saver status is to display a large full-screen picture. From the perspective of QT, we need to create a screensaver class and make it as an event agent for the application, if there is a mouse or keyboard event, then refresh the timer, if more than 30s did not receive the relevant event, the Full Screen display window. Here is a code example to explain.

Construct a screensaver class Cscreensaver:

cscreensaver.h file

#ifndef  CSCREENSAVER#define CSCREENSAVER  #ifndef  qobject_h#include <qobject># endif class qtimer;class qlabel;class cscreensaver : public qobject{     q_object public:    cscreensaver (QObject *parent =  NULL);     ~cscreensaver (); protected slots:    void  Slot_timeout ();  protected:    //Initialize screensaver parameter     void init ();          //event Receive handler function, called by Installeventfilter Caller when receiving an event      bool eventfilter (qobject *watched, qevent *event);      private:    //Timers     QTimer   *timer;          //objects for display-preserving images     QLabel   *label;          //Initial screensaver wait Time out     static const unsigned wait_time  = 30000;};   #endif

cscreensaver.cpp file

#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;     //Reading screensaver configuration      qsettings settings (Qapplication::applicationdirpath ()  +  "/config.ini",  Qsettings::iniformat);     settings.begingroup ("SCREENSAVER");     if   (Settings.contains ("Interval")     {         Bool ok;        waitinterval =&nbsP;settings.value ("Interval"). Touint (&ok);        if  (!ok)             waitInterval = WAIT_TIME;     }         if  (Settings.contains (" Picpath "))     {        urlPath =  Settings.value ("Picpath"). toString ();     }    settings.endgroup ();      //set up and start the timer. If more than 30s, the display is guaranteed and no longer triggered until the timer is refreshed again     timer = new QTimer;     timer->setsingleshot (True);     connect (Timer, signal (timeout ()),  this,  slot (Slot_timeout ()));     timer->start (Waitinterval);      //Screensaver window     qrect screenrect = qapplication::d esktop ()->screenGeomEtry (0);     label = new qlabel ();    label-> Setgeometry (Screenrect);     label->setwindowflags (Qt::framelesswindowhint);     //Stretch background picture     label->setscaledcontents (true);          qpixmap pmp;    pmp.load (URLPath);     Label->setpixmap (PMP);     label->hide ();}  bool cscreensaver::eventfilter (qobject *obj, qevent *event) {     Judging event Type     if  (Event->type ()  == qevent::keypress | |  event->type ()  == qevent::mousemove        | |  event->type ()  == qevent::mousebuttonpress)  {         //a mouse or keyboard event resets the Timer        timer->start ();         label->hide ();    }      return qobject::eventfilter (obj, event);}  void cscreensaver::slot_timeout () {    //display     label-> Activatewindow ();     label->show ();}

In the main.cpp file

#include <QApplication> ... #include <cscreensaver.h> int main (int argc, char *argv[]) {qapplication app (arg     c, argv);         ...    Cscreensaver ScreenSaver;         Global receive and delegate processing event App.installeventfilter (&screensaver); ... return app.exec ();}

This article is from the "CuZn Station" blog, please be sure to keep this source http://cuzn1024.blog.51cto.com/9214612/1619340

[QT] Making software-level Screensavers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.