qt判断用户是否正在使用当前应用程序
2017-09-29 19:58
681 查看
如何在qt程序中判断当前应用程序是否被用户使用呢?我们先了解一下QApplication这个类
简单的说,QApplication 类管理图形用户界面应用程序的控制流和主要设置。可以说 QApplication是Qt的整个后台管理的命脉
它包含主事件循环,在其中来自窗口系统和其它资源的所有事件被处理和调度。它也处理应用程序的初始化和结束,并且提供对话管理。它也处理绝大多数系统范围和应用程序范围的设置。
QApplication类 中函数notify
负责事件的通知和分发,这样 我们重写这个函数就可以啦。
//主要功能,判断用户是否在当前应用程序中触发了鼠标事件或者键盘事件
源码
头文件,listener.h
#pragma
once
#include<time.h>
#include
<QApplication>
#include<QTimer>
class Listener :public
QApplication
{
Q_OBJECT
public:
Listener(int&argc,
char**argv);
~Listener();
signals:
void
idle();
private:
bool
notify(QObject *receiver,
QEvent *event);
time_t
m_lastActive;
QTimer *m_timer;
private
slots:
int
TimeEvent();
};
Cpp文件
#include
"Listener.h"
Listener::Listener(int&argc,
char**argv)
: QApplication(argc,argv)
{
m_lastActive =time(NULL);
m_timer =new
QTimer(this);
m_timer->start(1000);
connect(m_timer,
SIGNAL(timeout()),this,
SLOT(TimeEvent()));
}
Listener::~Listener()
{
}
bool Listener::notify(QObject *receiver,
QEvent *event)
{
if (event->type()==
QEvent::MouseMove
||event->type()==
QEvent::KeyPress)
{
m_lastActive =time(NULL);
}
return
QApplication::notify(receiver,
event);
}
int Listener::TimeEvent()
{
time_t
now =time(NULL);
int
elapsed =now - m_lastActive;
if (m_lastActive>0&&elapsed> 20)
{
emit
idle();
m_lastActive =time(NULL);
}
return0;
}
简单的说,QApplication 类管理图形用户界面应用程序的控制流和主要设置。可以说 QApplication是Qt的整个后台管理的命脉
它包含主事件循环,在其中来自窗口系统和其它资源的所有事件被处理和调度。它也处理应用程序的初始化和结束,并且提供对话管理。它也处理绝大多数系统范围和应用程序范围的设置。
QApplication类 中函数notify
负责事件的通知和分发,这样 我们重写这个函数就可以啦。
//主要功能,判断用户是否在当前应用程序中触发了鼠标事件或者键盘事件
源码
头文件,listener.h
#pragma
once
#include<time.h>
#include
<QApplication>
#include<QTimer>
class Listener :public
QApplication
{
Q_OBJECT
public:
Listener(int&argc,
char**argv);
~Listener();
signals:
void
idle();
private:
bool
notify(QObject *receiver,
QEvent *event);
time_t
m_lastActive;
QTimer *m_timer;
private
slots:
int
TimeEvent();
};
Cpp文件
#include
"Listener.h"
Listener::Listener(int&argc,
char**argv)
: QApplication(argc,argv)
{
m_lastActive =time(NULL);
m_timer =new
QTimer(this);
m_timer->start(1000);
connect(m_timer,
SIGNAL(timeout()),this,
SLOT(TimeEvent()));
}
Listener::~Listener()
{
}
bool Listener::notify(QObject *receiver,
QEvent *event)
{
if (event->type()==
QEvent::MouseMove
||event->type()==
QEvent::KeyPress)
{
m_lastActive =time(NULL);
}
return
QApplication::notify(receiver,
event);
}
int Listener::TimeEvent()
{
time_t
now =time(NULL);
int
elapsed =now - m_lastActive;
if (m_lastActive>0&&elapsed> 20)
{
emit
idle();
m_lastActive =time(NULL);
}
return0;
}
相关文章推荐
- QT 判断当前焦点是否在某控件上(使用hasFocus函数,毕竟QT是DIRECTUI,所以这个Focus到底代表了什么呢?)
- 判断某个窗口是否是活动窗口(当前正在使用、操作的窗口)
- js判断用户是否正在浏览当前页面
- 在Sharepoint 2013中,使用JS判断当前用户是否在某个组里面
- html5新功能2—使用HTML5里页面可见性接口判断用户是否正在观看你的页面
- Android之——判断当前应用程序是否是用户程序
- 使用HTML5里页面可见性接口判断用户是否正在观看你的页面
- Delphi判断文件是否正在被使用
- VC++中如何判断当前用户是否具有管理员权限
- 判断用户输入的数字是否为99,并将不是数字之和打印出来,巧妙使用break和continue。
- 判断文件是否正在被使用
- Android判断网络状态是否断开+Android完全关闭应用程序+ 本文讲述了Android中不同Activity之间的数据传递 — Bundle对象的实现(-)+Android中Bundle的使用示例
- moss中判断当前用户是否有访问某web的权限
- 使用SQL判断当前JOB是否在运行
- 交互式让用户输入一个文件路径,判断文件是否存在,若存在判断文件类型,若文件是普通文件,不能使用wc。
- 检测手机用户安装的应用程序是否有使用某权限
- 判断文件是否正在使用
- 如何判断当前用户是否是系统管理员
- 判断当前用户是否为管理员组
- 如何使PC上运行的程序不能退出,并且用户只能使用当前正在运行的程序。不能使用PC上的其他程序