Qt定时器互关问题
学习Qt有一段时间了,基本都是帮人解决问题,但是自己的问题也没解决好。
假设两个定时器QTimer,设为A、B,连接timeout信号到插槽SlotA、SlotB。
SlotA的主要任务是打印A字符串
void SlotA
{
///....暂时省略
static int aCount = 0 ;
qDebug( "This is SlotA %d" , aCount++ );
////...暂时省略
}
SlotB的主要任务是打印B字符串
void SlotB
{
///....暂时省略
static int bCount = 0;
qDebug( "This is SlotB %d" , bCount++ );
////...暂时省略
}
A和B的timeout时间设为2000ms,而且A比B先开始,
A.start( 2000 );
B.start( 2000 );
问题1如下:
SlotA的主要任务是打印A字符串
void SlotA
{
///....暂时省略
static int aCount = 0 ;
qDebug( "This is SlotA %d" , aCount++ );
if( aCount > 100 )
B.stop();
}
SlotB的主要任务是打印B字符串
void SlotB
{
///....暂时省略
static int bCount = 0;
qDebug( "This is SlotB %d" , bCount++ );
if( bCount >100 )
A.stop();
}
A和B的timeout时间设为2000ms,而且A比B先开始,
A.start( 2000 );
B.start( 2000 );
A和B的定时器事件会形成队列执行,执行到一定程序导致A、B停止。
但是最后的输出结果是怎么样呢?
问题2如下:
SlotA的主要任务是打印A字符串
void SlotA
{
///....暂时省略,但是可以肯定,这个操作会操过2秒
static int aCount = 0 ;
qDebug( "This is SlotA %d" , aCount++ );
if( aCount > 100 )
B.stop();
}
SlotB的主要任务是打印B字符串
void SlotB
{
///....暂时省略,但是可以肯定,这个操作会操过2秒
static int bCount = 0;
qDebug( "This is SlotB %d" , bCount++ );
if( bCount >100 )
A.stop();
}
A和B的timeout时间设为2000ms,而且A比B先开始,
A.start( 2000 );
B.start( 2000 );
A和B的定时器事件会形成队列执行,执行到一定程序导致A、B停止。
但是最后的输出结果是怎么样呢?