Qt implements timeout lock screen

Use Qt to realize the function of timeout lock screen (industrial computer touch screen), when the hand does not touch the screen for a long time, the program will display the lock screen window when the program times out.

1. Effect

The main window times out to show the lockscreen window:

 

The system window timed out to show the lock screen window:

2. Implementation ideas

First start a thread for QTimer timer timing, first enter the software to start the timer timing, and the lock screen window will be displayed when the timeout expires; if the hand touches the screen, it will trigger a mouse click event, and the timing will stop; if the hand leaves the screen, it will trigger a mouse release event, and then start timing again.

Because it is a multi-window application software, it needs to monitor global mouse events, so it needs to subclass the QApplication class.  

Note: With a little modification, it can be used on the application software on the PC side to further detect mouse movement events and keyboard events.

The benefits of this article, the fee to receive Qt development learning materials package, technical video, including (C++ language foundation, Qt programming introduction, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓ See below

3. Realize the code

The class that monitors global mouse events is GlobalApplication, which also implements the functions of opening a thread for timer timing and displaying the lock screen window over time.

The content of the globalapplication.h file is as follows:

#ifndef GLOBALAPPLICATION_H
#define GLOBALAPPLICATION_H

#include <QApplication>
#include <QMouseEvent>
#include <QThread>
#include <QTimer>
#include <QDebug>
#include "widget_lock.h"

class GlobalApplication : public QApplication
{
    Q_OBJECT

public:
     GlobalApplication(int&argc,char **argv);
     ~GlobalApplication();

     bool notify(QObject*, QEvent *);

private slots:
    void onTimeOut(); //超时锁屏槽函数

private:
    Widget_Lock *m_myLockWindow; //锁屏窗口
    QObject *m_currWinObj; //鼠标事件发生时的当前所在窗口

    QTimer *m_pTimer; //定时器
    QThread *m_pThread; //定时器线程
};

#endif // GLOBALAPPLICATION_H

Note: Widget_Lock is a lock screen window class. The specific implementation code will not be posted here due to space issues, and can be replaced with your own window to display.

The content of the globalapplication.cpp file is as follows:

#include "globalapplication.h"

GlobalApplication::GlobalApplication(int &argc,char **argv):
QApplication(argc,argv)
{
    m_currWinObj = new QObject(); //初始化鼠标事件发生时的当前所在窗口

    //初始化定时器线程和定时器
    m_pThread = new QThread(this);
    m_pTimer = new QTimer();
    m_pTimer->moveToThread(m_pThread);
    m_pTimer->setInterval(4000);
    connect(m_pThread, SIGNAL(started()), m_pTimer, SLOT(start()));
    connect(m_pTimer, SIGNAL(timeout()),this,SLOT(onTimeOut()));
    //开启线程,调用定时器的start()
    m_pThread->start();

    m_myLockWindow = new Widget_Lock(); //初始化锁屏窗口
    m_myLockWindow->m_unlockFlag = true; //初始化解除锁屏成功标志
}

GlobalApplication::~GlobalApplication()
{
    //彻底结束线程(单独的一个quit()无法彻底结束进程)
    m_pThread->requestInterruption();
    m_pThread->quit();
    m_pThread->wait();
}

bool GlobalApplication::notify(QObject *obj, QEvent *e)
{
    //因为鼠标点击或滑动到任何窗口部件都会进入该函数,所以根据objectName()过滤窗口部件
    if(obj->objectName().right(1) == "W") //cleanW mainW
    {
        //工控机是触摸屏,触摸屏幕和滑动屏幕都会点击屏幕,所以这里只检测鼠标点击事件
        if(e->type() == QEvent::MouseButtonPress) //不区分左右键按下了
        {
            //qDebug() << "objName:" << obj->objectName();
            //qDebug()<<"press stop";
            m_pThread->quit(); //线程结束后,则在此线程的定时器会自动停止计时
        }
        else if(e->type() == QEvent::MouseButtonRelease)
        {
            //qDebug()<<"release start";
            m_pThread->start(); //线程开始,会发送started()信号,启动定时器的start()槽函数
        }

        m_currWinObj = obj; //获得鼠标事件发生时的当前所在窗口
    }

    return QApplication::notify(obj,e);
}

//超时锁屏槽函数
void GlobalApplication::onTimeOut()
{
    //m_unlockFlag为false,表示锁屏窗口未关闭,则即使再超时也不会再显示锁屏窗口
    if(m_myLockWindow->m_unlockFlag == true) //当密码输入正确后,关闭锁屏窗口,则会将m_unlockFlag设置为true
    {
        qDebug()<<"锁屏";
        //创建锁屏窗口对象,并显示该窗口
        m_myLockWindow = new Widget_Lock(static_cast<QWidget *>(m_currWinObj));
        m_myLockWindow->show();

        //这行代码要放在最后,因为在构建对象时,会自动设置m_unlockFlag,所以在构建后要复位m_unlockFlag
        m_myLockWindow->m_unlockFlag = false;
    }
    else
        qDebug()<<"重复锁屏";
}

Then replace the Application a(argc, argv); in the main function with: GlobalApplication a(argc, argv); so that when you click the mouse in any window, you can respond and get the current window when the mouse event occurs. When the timeout expires, the lock screen window will be displayed in the current window.

The article is transferred from the blog garden (fengMisaka): Qt implements timeout lock screen - fengMisaka - Blog Garden

The benefits of this article, the fee to receive Qt development learning materials package, technical video, including (C++ language foundation, Qt programming introduction, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓ See below

Guess you like

Origin blog.csdn.net/QtCompany/article/details/131903508