当前位置:网站首页>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
}
边栏推荐
- Zipkin is easy to use
- Finishing the interview essentials of secsha system!!!
- Makefile基本原理
- Openshift deployment application
- 汉诺塔问题的求解与分析
- Sqli labs level 8 (Boolean blind note)
- Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
- Linux安装Oracle Database 19c RAC
- Find the node with the smallest value range in the linked list and move it to the front of the linked list
- Minecraft空岛服开服
猜你喜欢
随机推荐
sqli-labs第12关
Sqli labs level 8 (Boolean blind note)
sqli-labs(POST类型注入)
Sqli labs Level 2
Web安全--核心防御机制
Introduction to the basic concept of queue and typical application examples
Qt的connect函数和disconnect函数
Routing foundation - dynamic routing
Installing Oracle database 19C RAC on Linux
Nacos download, start and configure MySQL database
gocv图片裁剪并展示
Luogu greedy part of the backpack line segment covers the queue to receive water
一个经典约瑟夫问题的分析与解答
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Minecraft plug-in service opening
Web security -- Logical ultra vires
C#钉钉开发:取得所有员工通讯录和发送工作通知
Minecraft群組服開服
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
STM32 new project (refer to punctual atom)









