when i executing this Qtimer it says "invalid use of 'this' in non-member function"
QTimer *timerStart( )
{
QTimer* timer = new QTimer( );
Ball *b = new Ball();
QObject::connect(timer,SIGNAL(timeout()),b,SLOT(move()));
//timer->start( timeMillisecond );
timer->start(15);
return timer;
}
my ball.h file
class Ball: public QObject, public QGraphicsRectItem{
Q_OBJECT
public:
// constructors
Ball(QGraphicsItem* parent=NULL);
// public methods
double getCenterX();
public slots:
// public slots
void move();
private:
// private attributes
double xVelocity;
double yVelocity;
int counter = 0;
QTimer timerStart( );
// private methods
void stop();
void resetState();
void reverseVelocityIfOutOfBounds();
void handlePaddleCollision();
void handleBlockCollision();
};
#endif // BALL_H
the move() function is in the same class. what i want to do is stop the returned timer upon a if condition is satisfied.
when i issue this code in Ball::Ball constructor in Cpp it works fine. the ball is moving.
QTimer* timer = new QTimer();
timer->setInterval(4000);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(15);
but when i add Qtimer *timerStart beyond the Ball::Ball constructor, iT doesnt work
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…