当前位置:网站首页>Qt QTimer类
Qt QTimer类
2022-07-02 06:30:00 【代码海贼团船长】
Header:
#include
CMake:
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) qmake: QT += core Inherits: QObject
qmake:
QT+=core
继承自:QObject
公有函数(Public Functions)
QTimer(QObject *parent = nullptr);
QTimer类的构造函数。
virtual ~QTmer();
QTimer类的析构函数。
QMetaObject::Connection callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
这个函数是创建从 timeout() 信号到槽的 connectionType 类型的连接,并返回连接的句柄。提供此方法是为了方便, 它相当于调用 QObject::connect(timer, &QTimer::timeout, timer, slot, connectionType)。
QMetaObject::Connection callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
它相当于调用QObject::connect(timer, &QTimer::timeout, context, slot, connectionType).
QMetaObject::Connection callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
它相当于调用QObject::connect(timer, &QTimer::timeout, receiver, slot, connectionType).
int interval() const
获取定时器触发间隔时间。
std::chrono::milliseconds intervalAsDuration() const
获取定时器触发间隔时间,类型为std::chrono::milliseconds。
bool isActive() const
定时器在运行返回真,否则返回假。
bool isSingleShot() const
定时器是否为单次触发。
int remainingTime() const
获取定时器的剩余时间。
std::chrono::milliseconds remainingTimeAsDuration() const
获取定时器的剩余时间,类型为std::chrono::milliseconds。
void setInterval(int msec)
设置定时器的间隔时间,单位为毫秒。
void setInterval(std::chrono::milliseconds value)
设置定时器的间隔时间,类型为std::chrono::milliseconds。
void setSingleShot(bool singleShot)
设置定时器是否为单次触发。
void setTimerType(Qt::TimerType atype)
设置定时器的精度。
void start(std::chrono::milliseconds msec)
开启定时器。
int timerId() const
获取定时器的ID。
Qt::TimerType timerType() const
获取定时器的精度。
性质(Properties)
active:bool
read-only
如果定时器运行,这个布尔型性质返回真,否则返回假。
interval:int
此属性是定时器的触发间隔时间,单位为毫秒。
这里默认设置为0,一旦窗口系统事件队列中的所有事件都已经被处理完,一个时间间隔为0的QTimer就会触发。
remainingTime:int
read-only
此属性保存剩余时间,单位为毫秒。
返回计时器的剩余值,单位为毫秒,直到超时。如果计时器不活动,返回的值将为-1。如果计时器过期,返回的值将为0。
singleShot : bool
此属性是计时器的单次触发属性,若为单次触发,值为真,否则为假。
timerType : Qt::TimerType
此属性是定时器的触发精度,默认值为Qt::CoarseTimer,详见Qt::TimerType。
公有槽(Public Slots)
void start()
开启定时器。
void start(int msec)
开启定时器并设置时间间隔。
void stop()
关闭定时器
信号(Signals)
void timeout()
定时器触发信号。
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
timer->start(1000);
// 此处的QOverload<>::of用于区分update的重载函数
静态公有函数(Static Public Members)
void singleShot(int msec, const QObject *receiver, const char *member)
单次触发函数。
void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
void singleShot(int msec, const QObject *receiver, PointerToMemberFunction method)
void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method)
void singleShot(int msec, Functor functor)
void singleShot(int msec, Qt::TimerType timerType, Functor functor)
void singleShot(int msec, const QObject *context, Functor functor)
void singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor)
void singleShot(std::chrono::milliseconds msec, const QObject *receiver, const char *member)
void singleShot(std::chrono::milliseconds msec, Qt::TimerType timerType, const QObject *receiver, const char *member)
重载保护函数(Reimplemented Protected Functions)
virtual void timerEvent(QTimerEvent *e) override
重载定时器事件。使用此重写后,
枚举Qt::TimerType
Constant | Value | Description |
---|---|---|
Qt::PreciseTimer | 0 | 精准定时器 |
Qt::CoarseTimer | 1 | 普通定时器 |
Qt::VeryCoarseTimer | 2 | 粗糙定时器 |
例:
1. 循环触发某一个任务:在应用内显示当前时间。
QTimer *mpTimer = new QTimer(this);
mpTimer->callOnTimeout(this,QOverload<>::of(&MainWindow::updateTime));
mpTimer->start(1000);//间隔触发时间为1秒。
private slots:
void Example::updateTime()
{
qDebug()<<"当前时间为"<<QDateTime::currentDateTime();
}
2. 实现一个延时函数
void delayMs(int ms) //毫秒级的延时
{
QEventLoop loop;//定义一个新的事件循环
QTimer::singleShot(Ms, &loop, SLOT(quit()));//创建单次定时器,槽函数为事件循环的退出函数
loop.exec();//事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出
}
边栏推荐
- Pointer initialization
- What are the platforms for selling green label domain names? What is the green label domain name like?
- Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
- Sentinel 简单使用
- HackTheBox-Gunship
- kubeadm部署kubernetes v1.23.5集群
- How to uninstall SQL Server cleanly
- Comparable,Comparator,Clonable 接口使用剖析
- Dip1000 implicitly tagged with fields
- Sqli labs level 12
猜你喜欢
[blackmail virus data recovery] suffix Hydra blackmail virus
Openfeign is easy to use
commands out of sync. did you run multiple statements at once
Nacos 下载启动、配置 MySQL 数据库
[untitled]
Simple implementation scheme of transcoding and streaming (I)
顺序表基本功能函数的实现
Realization of basic function of sequence table
Minecraft模组服开服
Googlenet network explanation and model building
随机推荐
16: 00 interview, came out at 16:08, the question is really too
sqli-labs第1关
Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
ARP and ARP Spoofing
Minecraft安装资源包
Matlab other
Short video with goods source code, double-click to zoom in when watching the video
DWORD ptr[]
旋转链表(图解说明)
Minecraft群组服开服
Realize bidirectional linked list (with puppet node)
How to build the alliance chain? How much is the development of the alliance chain
Jz-061-serialized binary tree
STM32 new project (refer to punctual atom)
IP协议与IP地址
sqli-labs第8关(布尔盲注)
整理秒杀系统的面试必备!!!
c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)
Minecraft群組服開服
Valin cable: BI application promotes enterprise digital transformation