当前位置:网站首页>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 WLAN related knowledge points
- Storage space modifier in CUDA
- 20201002 VS 2019 QT5.14 开发的程序打包
- 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.)
- Uploading attachments using Win32 in Web Automation
- pytest(1) 用例收集规则
- 稀疏数组(非线性结构)
- CUDA中的异步数据拷贝
- eslint配置代码自动格式化
- FE - weex 开发 之 使用 weex-ui 组件与配置使用
猜你喜欢
js中正则表达式的使用
Sparse array (nonlinear structure)
Find the highest value of the current element Z-index of the page
Code skills - Controller Parameter annotation @requestparam
Amazon AWS data Lake Work Pit 1
实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了
栈(线性结构)
Cglib agent - Code enhancement test
Introduce two automatic code generators to help improve work efficiency
Summary of advertisement business bug replay
随机推荐
Sentinel rules persist to Nacos
AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
找到页面当前元素z-index最高的数值
(第一百篇BLOG)写于博士二年级结束-20200818
DeprecationWarning: .ix is deprecated. Please use.loc for label based indexing or.iloc for positi
web自动化切换窗口时报错“list“ object is not callable
ctf三计
Sentinel 阿里开源流量防护组件
CUDA与Direct3D 一致性
RestTemplate请求时设置请求头,请求参数,请求体。
Introduce two automatic code generators to help improve work efficiency
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
Redis——大Key问题
FE - weex 开发 之 使用 weex-ui 组件与配置使用
标签属性disabled selected checked等布尔类型赋值不生效?
【每日一题】—华为机试01
Three suggestions for all students who have graduated and will graduate
ZZQ的博客目录--更新于20210601
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
Summary of advertisement business bug replay