当前位置:网站首页>QT qtimer class
QT qtimer class
2022-07-02 08:50:00 【Code pirate captain】
Header:
#include
CMake:
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) qmake: QT += core Inherits: QObject
qmake:
QT+=core
Inherited from :QObject
Public function (Public Functions)
QTimer(QObject *parent = nullptr);
QTimer Class constructor .
virtual ~QTmer();
QTimer Destructor of class .
QMetaObject::Connection callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
This function is created from timeout() Signal to slot connectionType Type of connection , And return the handle of the connection . This method is provided for convenience , It's equivalent to calling QObject::connect(timer, &QTimer::timeout, timer, slot, connectionType).
QMetaObject::Connection callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
It's equivalent to calling QObject::connect(timer, &QTimer::timeout, context, slot, connectionType).
QMetaObject::Connection callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection)
It's equivalent to calling QObject::connect(timer, &QTimer::timeout, receiver, slot, connectionType).
int interval() const
Get the timer trigger interval .
std::chrono::milliseconds intervalAsDuration() const
Get the timer trigger interval , The type is std::chrono::milliseconds.
bool isActive() const
The timer returns true when running , Otherwise return false .
bool isSingleShot() const
Whether the timer is a single trigger .
int remainingTime() const
Get the remaining time of the timer .
std::chrono::milliseconds remainingTimeAsDuration() const
Get the remaining time of the timer , The type is std::chrono::milliseconds.
void setInterval(int msec)
Set the interval between timers , The unit is millisecond .
void setInterval(std::chrono::milliseconds value)
Set the interval between timers , The type is std::chrono::milliseconds.
void setSingleShot(bool singleShot)
Set whether the timer is a single trigger .
void setTimerType(Qt::TimerType atype)
Set the accuracy of the timer .
void start(std::chrono::milliseconds msec)
Turn on timer .
int timerId() const
Get the of the timer ID.
Qt::TimerType timerType() const
Get the accuracy of the timer .
nature (Properties)
active:bool
read-only
If the timer runs , This Boolean property returns true , Otherwise return false .
interval:int
This attribute is the trigger interval of the timer , The unit is millisecond .
The default setting here is 0, Once all the events in the window system event queue have been processed , One time interval is 0 Of QTimer It will trigger .
remainingTime:int
read-only
This attribute saves the remaining time , The unit is millisecond .
Return the remaining value of the timer , The unit is millisecond , Until timeout . If the timer is inactive , The returned value will be -1. If the timer expires , The returned value will be 0.
singleShot : bool
This attribute is the single trigger attribute of the timer , If it is a single trigger , It's worth it , Otherwise it is false .
timerType : Qt::TimerType
This attribute is the trigger accuracy of the timer , The default value is Qt::CoarseTimer, See Qt::TimerType.
Public slot (Public Slots)
void start()
Turn on timer .
void start(int msec)
Turn on the timer and set the time interval .
void stop()
off timer
The signal (Signals)
void timeout()
Timer trigger signal .
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
timer->start(1000);
// Here QOverload<>::of Used to distinguish update Overloaded functions for
Static public functions (Static Public Members)
void singleShot(int msec, const QObject *receiver, const char *member)
Single trigger function .
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)
Overload protection function (Reimplemented Protected Functions)
virtual void timerEvent(QTimerEvent *e) override
Reload timer events . After using this rewrite ,
enumeration Qt::TimerType
Constant | Value | Description |
---|---|---|
Qt::PreciseTimer | 0 | Precision timer |
Qt::CoarseTimer | 1 | Ordinary timer |
Qt::VeryCoarseTimer | 2 | Rough timer |
example :
1. Loop triggers a task : Display the current time in the app .
QTimer *mpTimer = new QTimer(this);
mpTimer->callOnTimeout(this,QOverload<>::of(&MainWindow::updateTime));
mpTimer->start(1000);// The interval trigger time is 1 second .
private slots:
void Example::updateTime()
{
qDebug()<<" The current time is "<<QDateTime::currentDateTime();
}
2. Implement a delay function
void delayMs(int ms) // Millisecond delay
{
QEventLoop loop;// Define a new event loop
QTimer::singleShot(Ms, &loop, SLOT(quit()));// Create a single timer , The slot function is the exit function of the event loop
loop.exec();// The event loop starts executing , The program will be stuck here , Until the time is up , This cycle is exited
}
边栏推荐
猜你喜欢
Openshift deployment application
OpenShift构建镜像
路由基础—动态路由
Function ‘ngram‘ is not defined
Sentinel 简单使用
Installing Oracle database 19C RAC on Linux
[blackmail virus data recovery] suffix Crylock blackmail virus
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
commands out of sync. did you run multiple statements at once
Luogu greedy part of the backpack line segment covers the queue to receive water
随机推荐
将一串数字顺序后移
Openfeign is easy to use
File upload and download performance test based on the locust framework
[untitled]
Synchronize files using unison
IP协议与IP地址
Kubedm deploys kubernetes v1.23.5 cluster
commands out of sync. did you run multiple statements at once
OpenShift 容器平台社区版 OKD 4.10.0部署
寻找链表中值域最小的节点并移到链表的最前面
Viewing JS array through V8
Minecraft group service opening
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
Tensorflow2 keras classification model
一、Qt的核心类QObject
C call system sound beep~
sqli-labs第1关
Kubesphere virtualization KSV installation experience
Dip1000 implicitly tagged with fields
PCL calculates the intersection of three mutually nonparallel planes