当前位置:网站首页>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
边栏推荐
- Summary of advertisement business bug replay
- 10 erreurs classiques de MySQL
- [self cultivation of programmers] - Reflection on job hunting Part II
- automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
- ZZQ的博客目录--更新于20210601
- It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
- 提高用户体验 防御性编程
- Shardingsphere JDBC
- Code skills - Controller Parameter annotation @requestparam
- virtualenv和pipenv安装
猜你喜欢
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
unittest.TextTestRunner不生成txt测试报告
【张三学C语言之】—深入理解数据存储
Summary of WLAN related knowledge points
Redis——Cluster数据分布算法&哈希槽
Redis——大Key問題
Redis——大Key问题
Name six schemes to realize delayed messages at one go
自学table au
日志(常用的日志框架)
随机推荐
Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
数据科学【八】:SVD(一)
Storage space modifier in CUDA
链表(线性结构)
Common means of modeling: combination
广告业务Bug复盘总结
Shardingsphere JDBC
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
Sublime Text 配置php编译环境
CUDA中的函数执行空间说明符
pytest(1) 用例收集规则
稀疏数组(非线性结构)
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
队列(线性结构)
日期时间API详解
CUDA用户对象
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.)
【每日一题】—华为机试01
ctf三计
浅谈三点建议为所有已经毕业和终将毕业的同学