Recently, Qt is used to implement functions similar to screen saver. When the mouse stays at a position for a certain period of time, the program will automatically jump into other interfaces (used to display some information ), when the mouse moves, the screen is switched back, and the effect is similar to screen protection.
The implementation idea is as follows:
Idea 1:
Inherit the QApplication class and override
Function to obtain the QEvent. If it is a mouse movement event, the timer is stopped. Otherwise, the timer is started. When the timer reaches a certain time, the slot event is triggered and the screen is switched.When starting the timer, you must first determine whether the timer has been started. If the timer has been started, do not start it again.
You also need to define a function
In main. in cpp, the defined form is passed in, so that the form can be operated and the signal and slot can be bound, here, my slot function is implemented in the cpp of the form.The Code is as follows:
Main. cpp
#include <QtGui/QApplication>#include "fullscreenwindow.h"#include "application.h"int main(int argc, char *argv[]){ Application a(argc, argv); FullScreenWindow w; a.setWindowInstance(&w); w.show(); return a.exec();}
Application. h# Ifndef APPLICATION_H # define APPLICATION_H # include <QApplication> # include <QDebug> # include "fullscreenwindow. h "# include" qtimer. h "class Application: public QApplication {public: Application (int & argc, char ** argv); bool notify (QObject *, QEvent *); void setWindowInstance (FullScreenWindow * wnd ); private: FullScreenWindow * window; // Save the QTimer * timer;}; # endif // APPLICATION_H
Application. cpp# Include "application. h "Application: Application (int & argc, char ** argv): QApplication (argc, argv), window (0) {// create a QTimer object timer = new QTimer (); // set the number of milliseconds for the timer to send a timeout () signal timer-> setInterval (10000 ); // start timer-> start ();} void Application: setWindowInstance (FullScreenWindow * wnd) {window = wnd; // signal and slot connect (timer, SIGNAL (timeout (), wnd, SLOT (onTimerOut ();} bool Application: notify (QObject * Obj, QEvent * e) {if (e-> type () = QEvent: MouseMove) {if (window) {timer-> stop (); qDebug () <"stop"; window-> resize (500,500); // call the function} else {if (! Timer-> isActive () {timer-> start (); qDebug () <"start" ;}} return QApplication: Running y (obj, e );}
Fullscreenwindow. h#ifndef FULLSCREENWINDOW_H#define FULLSCREENWINDOW_H#include <QMainWindow>namespace Ui { class FullScreenWindow;}class FullScreenWindow : public QMainWindow{ Q_OBJECTpublic: explicit FullScreenWindow(QWidget *parent = 0); ~FullScreenWindow();private: Ui::FullScreenWindow *ui;private slots: void onTimerOut();};#endif // FULLSCREENWINDOW_H
Fullscreenwindow. cpp#include "fullscreenwindow.h"#include "ui_fullscreenwindow.h"FullScreenWindow::FullScreenWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::FullScreenWindow){ ui->setupUi(this);}FullScreenWindow::~FullScreenWindow(){ delete ui;}void FullScreenWindow::onTimerOut(){ this->resize(900,900);}
The result is: at the beginning of the interface, the size of the form is 400*300, and the size of the mouse changes to 500*500. If the mouse does not move for 10 seconds, the size of the form changes to 900*900. Of course, you can also implement the form switching operation in the onTimeOut function, that is, the screen saver effect.Download program
Idea 2:
Use the timer (1 s) and counter to obtain the current mouse position (x, y) when the form is declared. When the timer time is reached, obtain the mouse position (x1, y1) again ), if the mouse position changes, it indicates that the mouse is moving within 1 s, and the counter is cleared. If the mouse position remains unchanged, the counter is + 1 until the counter reaches a certain number, it indicates that the mouse has not been moved during this time, so you can access the switch interface. Of course, this approach is not very scientific, it is possible that the mouse moves within 1 s and then moves back to the previous position (a pixel is not bad). This situation cannot be determined, but this situation is unlikely to happen manually.
Summary:
The following problems occur during the implementation of IDEA 1: When the mouse is not on the current interface, or when the interface is minimized, the program treats the mouse as not moving, that is to say, when you move the mouse and arrive at the time, it will also enter the Screen Saver program, so the MouseMove in the QEvent obtained in the Application indicates moving the mouse over the program interface. This problem is still unsolved. We look forward to your answers.