惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
月光博客
月光博客
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
U
Unit 42
腾讯CDC
爱范儿
爱范儿
J
Java Code Geeks
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
B
Blog
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - StationaryTraveller

如何查找微软VS软件中的图标 SQLite 的自增(AUTOINCREMENT)强制要求列的数据类型必须是INTEGER Qt动态获取翻译文本 Windows判断某窗口是否被其他窗口完全覆盖 MFC实现一个通用模态进度条窗口 如何批量修改“后期生成事件”为“预生成事件” MFC对话框中如何给一个控件发送消息 Window剪切板 C语言根据函数指针偏移实现函数动态调用 Qt程序系统文本翻译 SVN创建分支 Qt多版本如何共存 Qt6编译的程序在某些win10系统报错 电脑忘机了用户名密码怎么办 CMFCToolTipCtrl的AddTool导致内存增加 MFC中CBitmap、CBrush、CFont、CPalette、CPen、CRgn删除GDI对象问题 实现从QListWidgett拖拽项到QTableWidget Windows系统下通过命令行获取进程指标 命令模式实现撤销和重做机制 避免溢出求平均值的算法 字节流转16进制字符串 PImpl:Pointer to Implementation C++单例
Qt事件过滤器实现空闲检测
StationaryTraveller · 2025-01-20 · via 博客园 - StationaryTraveller
class idleDectector : public QObject
{
    Q_OBJECT
public:
    explicit idleDectector(QObject *parent = nullptr);
    ~idleDectector();

signals:
    void idle();

protected:
    bool eventFilter(QObject* obj, QEvent* event) override;
    virtual void timerEvent(QTimerEvent*) override;
private:
    QDateTime lastOperationTime;
    int timerID;
};
idleDectector::idleDectector(QObject *parent)
    : QObject{parent}
    , timerID(0)
{
    lastOperationTime = QDateTime::currentDateTime();

    qApp->installEventFilter(this);

    if (Config::GetInstance().getIdleTime() != 0)
    {
        timerID = this->startTimer(1000);
    }
}

idleDectector::~idleDectector()
{
    if (0 != timerID)
    {
        this->killTimer(timerID);
    }
}

void idleDectector::timerEvent(QTimerEvent* event)
{
    if (timerID == event->timerId())
    {
        if ((QDateTime::currentSecsSinceEpoch() - lastOperationTime.toSecsSinceEpoch()) >
            (qint64)Config::GetInstance().getIdleTime() * 60)
        {
            emit idle();
            lastOperationTime = QDateTime::currentDateTime();
        }
    }
}

bool idleDectector::eventFilter(QObject* obj, QEvent* event)
{
    switch (event->type())
    {
    case QEvent::MouseMove:
    case QEvent::KeyPress:
    case QEvent::MouseButtonPress:
        lastOperationTime = QDateTime::currentDateTime();
        break;
    default:
        break;
    }

    return QObject::eventFilter(obj, event);
}

应用:

MainWindow连接信号和槽

connect(&mDecector, &idleDectector::idle, this, &MainWindow::onIdle);