我已经启动Timer以等待某些条件为真。 IF条件为真然后我停止计时器并且不希望超时信号发出或执行连接的插槽。但如果条件在指定时间内为假,那么它就可以发出信号timeout()。但无论如何,它总是发出超时信号。我也使用了blockSignals(true),但它不起作用。有人可以建议我。
void timerStart( QTimer* timer, int timeMillisecond )
{
timer = new QTimer( this );
timer->setInterval( timeMillisecond );
timer->setSingleShot( true );
connect( timer, SIGNAL( timeout() ), this, SLOT( noRespFrmServer( ) ) ) ;
//timer->start( timeMillisecond );
timer->start();
}
void timerStop( QTimer* timer )
{
connect( timer, SIGNAL( destroyed( timer ) ), this, SLOT( stopTimerbeforeTimeout( ) ) );
qDebug() << " we are in timer stop";
if( timer )
{
timer->stop();
timer->blockSignals( true );
delete timer;
}
}
同样在timerStop函数中,我试图发出被破坏的信号,但我得到了无法连接的响应。 pLease建议我。
答案 0 :(得分:2)
void timerStart( QTimer* timer, int timeMillisecond )
{
timer = new QTimer( this );
timer->setInterval( timeMillisecond );
timer->setSingleShot( true );
connect( timer, SIGNAL( timeout() ), this, SLOT( noRespFrmServer( ) ) ) ;
//timer->start( timeMillisecond );
timer->start();
}
这实际上并没有返回刚刚创建的计时器。你想要这样的东西:
QTimer *timerStart(int timeMillisecond )
{
QTimer* timer = new QTimer( this );
timer->setInterval( timeMillisecond );
timer->setSingleShot( true );
connect( timer, SIGNAL( timeout() ), this, SLOT( noRespFrmServer( ) ) ) ;
//timer->start( timeMillisecond );
timer->start();
return timer;
}
然后你可以将返回的计时器传递给stop函数,虽然我建议你使用deleteLater而不是直接删除它:
void timerStop( QTimer* timer )
{
qDebug() << " we are in timer stop";
if( timer )
{
qDebug() << " we are stopping the timer";
timer->stop();
timer->blockSignals( true );
timer->deleteLater();
}
}