当前位置:网站首页>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();//事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出
}
边栏推荐
- Sqli labs level 1
- Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
- Makefile Fundamentals
- 实现双向链表(带傀儡节点)
- Minecraft安装资源包
- sqli-labs第8关(布尔盲注)
- Don't know mock test yet? An article to familiarize you with mock
- gocv边界填充
- DWORD ptr[]
- Detailed explanation of NIN network
猜你喜欢

2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers

Sentinel easy to use

IP protocol and IP address

C language custom type enumeration, Union (clever use of enumeration, calculation of union size)

HCIA—应用层

Kubedm deploys kubernetes v1.23.5 cluster

Sqli labs level 8 (Boolean blind note)

File upload and download performance test based on the locust framework

Data asset management function

OpenShift构建镜像
随机推荐
Sqli labs level 8 (Boolean blind note)
Use C language to receive JSON strings
kubeadm部署kubernetes v1.23.5集群
Tcp/ip - transport layer
OpenShift 部署应用
Openfeign is easy to use
2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers
Web security -- core defense mechanism
sqli-labs(POST类型注入)
How to build the alliance chain? How much is the development of the alliance chain
Rotating linked list (illustration)
Causes of laptop jam
Sentinel 简单使用
Smart agriculture solutions smart agriculture system development
Viewing JS array through V8
IP protocol and IP address
Dip1000 runaway
Minecraft模组服开服
Sentinel easy to use
整理秒杀系统的面试必备!!!