注意事项:

1、QTimer's accuracy depends on the underlying operating system and hardware.Most platforms support an accuracy of 20 milliseconds; some provide more.

2、If Qt is unable to deliver the requested number of timer events, it will silently discard some.

问题:当开启一个定时器,设置时间间隔为50ms,它对应的QTimerEvnet处理函数要执行一段耗时的操作,如何让界面快速响应用户的鼠标事件呢

代码:

#include "QtGuiApplication2.h"
#include <QDebug>
#include <QTimerEvent>
#include <QDateTime>
#include <QTime>
#include <QThread> QtGuiApplication2::QtGuiApplication2(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
startTimer();
connect(ui.pushButton, &QPushButton::clicked, this, &QtGuiApplication2::showCurrentTime);
} void QtGuiApplication2::timerEvent(QTimerEvent *event)
{
//QThread::msleep(2000);这个和下面的效果是一样的
QTime qTime1 = QTime::currentTime();
while (qTime1.msecsTo(QTime::currentTime())<)
qDebug() << "do nothing just for test";
} void QtGuiApplication2::showCurrentTime()
{
ui.label->setText(QDateTime::currentDateTime().toString("hh:mm:ss.zzz"));
}

Ui:

当运行程序的时候,点击按钮发现textLabel的响应时间差不多是2秒,通过text显示的时间可以计算出来

解决方法:

void QtGuiApplication2::timerEvent(QTimerEvent *event)
{
//QThread::msleep(2000);
QTime qTime1 = QTime::currentTime();
while (qTime1.msecsTo(QTime::currentTime()) < ) {
QCoreApplication::processEvents(QEventLoop::AllEvents, );
qDebug() << "do nothing just for test";
} }

这个解决了操作耗时的情况,但是当你选择QThread::msleep(2000);那怎么都会卡了


疑问:

假设当前时刻为0;程序开始运行;100ms时刻,我按下按钮;此时事件队列里面应该有QTimerEvent;QTimerEvent;QMousePress这三个事件要处理,按理说Label的时间相应应该是4s左右啊,可能的解释就是If Qt is unable to deliver the requested number of timer events, it will silently discard some.因此它丢弃了QMousePress之前相同的QTimerEvent

此实验验证了这个观念:

#include <QtWidgets/QDialog>
#include <QTime>
#include "ui_QtGuiApplication2.h" class QtGuiApplication2 : public QDialog
{
Q_OBJECT public:
QtGuiApplication2(QWidget *parent = Q_NULLPTR);
protected:
void timerEvent(QTimerEvent *event) override;
void showCurrentTime();
private:
Ui::QtGuiApplication2Class ui;
int id;
QTime lastTime;
};
QtGuiApplication2::QtGuiApplication2(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
startTimer();
id = startTimer();
lastTime = QTime::currentTime();
} void QtGuiApplication2::timerEvent(QTimerEvent *event)
{
qDebug() << "current timer id: " << event->timerId();
if (event->timerId() == id) {
ui.label->setText(QString::number(lastTime.msecsTo(QTime::currentTime())));
lastTime= QTime::currentTime();
}
}

通过界面的Label显示基本都是1014左右;多出来的14毫秒就是执行50msQTimerEvent的输出语句和 lastTime= QTime::currentTime()的总耗时;

因此可以推测出:

当有个定时器在事件队列里有很多个QTimerEvent;那么当有另外一个事件A到了,在执行完当前的QTimerEvent之后它会丢弃A之前其他QTimerEvent;直接执行事件A

所以14毫秒就是执行50msQTimerEvent的输出语句和 lastTime= QTime::currentTime()的总耗时

QTimer的一些注意事项和探索的更多相关文章

  1. ArcSDE安装注意事项(二)

    上次安装的arcsde9.3后尝试往本机sde数据库中导入一个区局的数据库,但是每次都是导到一半就卡死不动,也没有报任何的错误,因此安装了arcsde9.3 sp2补丁,安装补丁后连接sde(非直连方 ...

  2. [收藏转贴]struct探索·extern "C"含义探索 ·C++与C的混合编程·C 语言高效编程的几招

    一.C/C++语言 struct深层探索 1.自然对界 struct是一种复合数据类型,其构成元素既可以是基本数据类型(如 int.long.float等)的变量,也可以是一些复合数据类型(如 arr ...

  3. Adapter优化方案的探索

    概要:使用Adapter的注意事项与优化方案本文的例子都可以在结尾处的示例代码连接中看到并下载,如果喜欢请star,如果觉得有纰漏请提交issue,如果你有更好的点子可以提交pull request. ...

  4. Qt 鼠标样式特效探索样例(一)——利用时间器调用QWidget.move()函数

    Qt 鼠标样式特效探索样例(一)       心血来潮,突然想在Qt里玩一把鼠标样式,想到在浏览网页时,经常看到漂亮的鼠标动画,于是今天摸索着乱写个粗糙的demo,来满足自己的好奇心. 效果图 方案要 ...

  5. [转]struct 用法深入探索

    struct用法深入探索 作者: Cloudward 1. struct的巨大作用 面对一个人的大型C/C++程序时,只看其对struct的使用情况我们就可以对其编写者的编程经验进行评估.因为一个大型 ...

  6. 《android开发艺术探索》读书笔记(七)--动画

    接上篇<android开发艺术探索>读书笔记(六)--Drawable No1: 自定义动画:派生一种新动画只需要继承Animation这个抽象类,然后重写它的initialize和app ...

  7. Android艺术开发探索第四章——View的工作原理(下)

    Android艺术开发探索第四章--View的工作原理(下) 我们上篇BB了这么多,这篇就多多少少要来点实战了,上篇主席叫我多点自己的理解,那我就多点真诚,少点套路了,老司机,开车吧! 我们这一篇就扯 ...

  8. Android开发艺术探索——第二章:IPC机制(上)

    Android开发艺术探索--第二章:IPC机制(上) 本章主要讲解Android的IPC机制,首先介绍Android中的多进程概念以及多进程开发模式中常见的注意事项,接着介绍Android中的序列化 ...

  9. Android开发艺术探索——新的征程,程序人生路漫漫!

    Android开发艺术探索--新的征程,程序人生路漫漫! 偶尔写点东西分享,但是我还是比较喜欢写笔记,看书,群英传看完了,是学到了点东西,开始看这本更加深入Android的书籍了,不知道适不适合自己, ...

随机推荐

  1. NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)

    组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...

  2. oracle rac的启动与停止

    引言:这写篇文章的出处是因为我的一名学生最近在公司搭建RAC集群,但对其启动与关闭的顺序和原理不是特别清晰,我在教学工作中也发现了很多学员对RAC知识了解甚少,因此我在这里就把RAC里面涉及到的最常用 ...

  3. Jumpserver 介绍

    安装jumperserver Sudo yum install –y git Su root Cd  /usr/local Mkdir jumpserver 安装等依赖包 yum -y install ...

  4. python cntl使用

    import sys 2 import time 3 import fcntl 4 5 class FLOCK(object): 6 7 def __init__(self, name): 8 sel ...

  5. django 初始化 介绍 生命周期

    安装好django,配置模板,静态文件 # 创建Django工程 django-admin startproject [工程名称] mysite - mysite # 对整个程序进行配置 - init ...

  6. Team Foundation Server 2010简体中文版

    文件名称:Team Foundation Server 2010简体中文版 文件大小:1.8 GBhttp://www.google.com/profiles/dedecms.com 下载地址: th ...

  7. FLIR ONE PRO热成像仪

    FLIR ONE PRO热成像仪 https://www.chiphell.com/thread-1774218-1-1.html

  8. LaTex与数学公式

    w(t) \longrightarrow \bigg[\frac{\sqrt{2\sigma ^2\beta}}{s+\beta}\bigg]  \longrightarrow \bigg[\frac ...

  9. 【linux】centos6.9设置etc0网卡开机自动获取ip

    在vm新安装的centos系统中,一般选择NAT来设置和主机共享局域网,通过ifconfig etc0 192.168.xx.xx 这种作法机器重启之后就会失效,所以可以使用更改文件的方式完成设置ce ...

  10. How HipChat Stores And Indexes Billions Of Messages Using ElasticSearch And Redis[转]

    This article is from an interview with Zuhaib Siddique, a production engineer at HipChat, makers of  ...