使用startTimer()启动kill计时器

时间:2018-03-03 09:13:57

标签: c++ qt

我有一个计时器,我在start()函数中启动。它控制一个timerEvent函数,但我无法阻止它。我试过killTimer(),但我不知道它的ID。

startTimer(60);
void Game::timerEvent(QTimerEvent *){}
killTimer(0);

该功能只是检查按键。 killTimer(0)不会返回错误,但每隔一个id都会返回错误,但它仍然不会杀死计时器。有没有办法通过函数获取计时器ID? 错误:QObject :: killTimer():错误:定时器id 1对于对象0x1c81bff8(游戏)无效,计时器尚未被杀死

编辑://小例子

start(){ 
scene->setSceneRect(0,0,1200,800);
setScene(scene);

timerId = this->startTimer(60);
}

//when the game ends, i call konec();

konec(){
 if(winner==1 || winnerCPU==1){
     removeAllItems(); // this function attemps to stop the timer
     scene->setSceneRect(0,0,1200,800);
     setScene(scene);
     }

     //i have this on a timer also, it stats with start() but i stop the 
     //timer in removeAllItems()
     // may this cause multiple timers IDs?
     QRectF rect = scene->sceneRect();
     rect.translate(avto->speed, 0);
     scene->setSceneRect(rect);

removeAllItems(){ /

timer->stop();
timer2->stop();
mtimer->stop();
shifter->stop();
time->stop();
move_timer->stop();
this->killTimer(timerId);



QList<QGraphicsItem *> stvari = scene->items();
for(int i=0, n=stvari.size();i<n;i++){
    if((typeid(*(stvari[i]))==typeid(Button)) || (typeid(*
(stvari[i]))==typeid(QTableView)) ||
            (typeid(*(stvari[i]))==typeid(Count)) || (typeid(*
(stvari[i]))==typeid(Linija)) ||
            (typeid(*(stvari[i]))==typeid(Player)) || (typeid(*
(stvari[i]))==typeid(Player2)) ||
            (typeid(*(stvari[i]))==typeid(Sred)))
            scene->removeItem(stvari[i]);}
}

1 个答案:

答案 0 :(得分:0)

  int timerId;
  timerId = startTimer(60);
  killTimer(timerId);