当前位置:网站首页>2020-9-23 use of QT timer qtimer class.
2020-9-23 use of QT timer qtimer class.
2022-07-02 06:34:00 【qq_ forty million nine hundred and thirty-eight thousand one hu】
stay Qt There are two ways to use timers in , One is to use QObiect Class timer ; One is to use QTimer class .
The accuracy of timers depends on the operating system and hardware , Most platforms support 20ms The accuracy of .
■、QObject Class timer
QObject It's all Qt Object's base class , It provides a basic timer . adopt QObject::startTimer(), A time interval of one millisecond can be used as a parameter to start the timer , This function returns a unique integer timer identifier . This timer starts at every time interval " Trigger ", Until the timer identifier is explicitly used to call QObject::killTimer() end .
When the timer is triggered , The application will send a QTimerEvent. In the event loop , The processor processes timer events in the order of the event queue . When the processor is busy processing other events , The timer can't handle it immediately .
QObject Class also provides the function of timing . Timer related member functions are :startTimer()、timeEvent()、killTimer().
QObject In the base class startTimer() and timerEvent() The prototype and description are as follows :
int QObject::startTimer(int interval);
Start a timer and return to the timer ID, If you can't start a timer , Will return 0. After the timer starts , every other interval The millisecond interval will trigger a timeout event , until killTimer() Called to delete the timer . If interval by 0, Then there is no window system event processing every time the timer event occurs .
virtual void QObject::timerEvent(QTimerEvent *event);
Virtual functions timerEvent() Overloaded to implement the user's timeout event handler . If more than one timer is running ,QTimerEvent::timerId() Used to find the specified timer , Operate on it .
When a timer event occurs , Virtual functions timerEvent() With QTimerEvent The event parameter class is called together , Overload this function to get timer events .
The usage of timer is as follows :
// The header file
class QNewObject : public QObject
{
Q_OBJECT
public:
QNewObject( QObject * parent = 0 );
virtual ~QNewObject();protected:
void timerEvent( QTimerEvent *event );
int m_nTimerId;
};// Source file
QNewObject::QNewObject( QObject * parent )
:QNewObject( parent )
{
m_nTimerId = startTimer(1000);
}QNewObject::~QNewObject()
{
if ( m_nTimerId != 0 )
killTimer(m_nTimerId);
}void QNewObject::timerEvent( QTimerEvent *event )
{
qDebug( "timer event, id %d", event->timerId() );
}
■、 Timer class QTimer
Timer class QTimer Provide a timer that transmits a signal when the timer is triggered , It provides a timeout event that triggers only once , The usual usage is as follows :
// Create timer
QTimer *testTimer = new QTimer(this);
// Connect the timer timeout signal to the slot ( Function function ) Connect
connect( testTimer, SIGNAL(timeout()), this, SLOT(testFunction()) );
// Start running timer , The timing interval is 1000ms
testTimer->start(1000);
...
// Stop the timer
if ( testTimer->isActive() )
testTimer->stop();
QTimer It also provides a simple function with only one timing singleShot().
A timer is 100ms Post trigger handler animateTimeout() And trigger only once . The code is as follows :
QTimer::singleShot( 100, this, SLOT(animateTimeout()) );
The second introduction :
The use of timer is very simple , We only need the following steps to complete the application of timer :
1. Generate a timer
QTimer *time_clock=new QTimer(parent);
2. Connect the signal and slot of this timer , Using a timer timeout()
connect(time_clock,SIGNAL(timeout()),this,SLOT(slottimedone()));
That is, it will be sent when the timing time comes timeout() The signal , triggering slottimedone() Slot to finish something
3. Start timer , And set the timing period
There are two kinds of timer timing :start(int time) and setSingleShot(true)
among start(int time) Yes means every time Seconds will restart the timer , The trigger timing can be repeated , Unless you use stop() Turn off the timer .
and setSingleShot(true) Only start the timer once
The former is often used in our projects .
time_clock->start(2000);
remarks : The usage of timer I found on the Chinese official website is in the following format :
QTimer *timer = new QTimer( myObject );
connect( timer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
timer->start( 2000, TRUE ); // 2 Second single trigger timer
About the last sentence ,timer->start( 2000, TRUE ); In my practical application , add TRUE The compiler will report an error , do not know why , It doesn't matter if you remove it . This problem remains , Maybe it can be solved later
边栏推荐
- CUDA中的存储空间修饰符
- 分布式事务 :可靠消息最终一致性方案
- 20201002 VS 2019 QT5.14 开发的程序打包
- Asynchronous data copy in CUDA
- 稀疏数组(非线性结构)
- Log (common log framework)
- Summary of advertisement business bug replay
- 2020-9-23 QT的定时器Qtimer类的使用。
- The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
- 计算属性普通函数写法 和 set get 写法
猜你喜欢

分布式事务 :可靠消息最终一致性方案

20201002 VS 2019 QT5.14 开发的程序打包

Decryption skills of encrypted compressed files

Sentinel rules persist to Nacos

Amazon AWS data Lake Work Pit 1

In depth understanding of JUC concurrency (I) what is JUC

实现strStr() II

Alibaba cloud MFA binding Chrome browser

Summary of advertisement business bug replay

日志(常用的日志框架)
随机推荐
Redis - hot key issues
MySql索引
深入学习JVM底层(三):垃圾回收器与内存分配策略
In depth understanding of JUC concurrency (I) what is JUC
深入学习JVM底层(二):HotSpot虚拟机对象
The difference between session and cookies
广告业务Bug复盘总结
TensorRT的命令行程序
ZZQ的博客目录--更新于20210601
递归(迷宫问题、8皇后问题)
一起学习SQL中各种join以及它们的区别
Pytest (1) case collection rules
[self cultivation of programmers] - Reflection on job hunting Part II
Asynchronous data copy in CUDA
eslint配置代码自动格式化
Is there a really free applet?
利用NVIDIA GPU将Minecraft场景渲染成真实场景
注解和反射详解以及运用
Cglib代理-代码增强测试
Flask-Migrate 检测不到db.string() 等长度变化