[Solved] how to stop the Qtimer upon a condition


Declare the QTimer as a member in you class

h file:

class Ball: public QObject, public QGraphicsRectItem{
{
  Q_OBJECT

  public:
    // constructor
    Ball(QGraphicsItem* parent=Q_NULLPTR);
    // control your timer
    void start();
    void stop();
    ...
  private:
    QTimer * m_poTimer;
}

Initiate the timer object in your constractor

cpp file:

Ball::Ball(QGraphicsItem* parent) :
      QGraphicsItem(parent)
{  
  m_poTimer = new QTimer(this); // add this as a parent to control the timer resource
  m_poTimer->setInterval(4000);

  connect(m_poTimer,SIGNAL(timeout()),this,SLOT(move()));
}

void Ball::start()
{
   // start timer
   m_poTimer->start();
}

void Ball::stop()
{
   // stop timer
   m_poTimer->stop();
}

2

solved how to stop the Qtimer upon a condition