2017-11-22 147 views
-1

私はこのようなメインコンストラクターにqtimerをセットアップしました。Qtクリエーターでqtimerを停止する

Ball::Ball(QGraphicsItem *parent): QGraphicsRectItem(parent), QObject(){ 
    // draw rect 
    setRect(0,0,50,50); 
    QBrush brush; 
    brush.setStyle(Qt::SolidPattern); 
    brush.setColor(Qt::red); 
    setBrush(brush); 

    // move up right initially 
    xVelocity = 0; 
    yVelocity = -5; 


    QTimer* timer = new QTimer(); 
    connect(timer,SIGNAL(timeout()),this,SLOT(move())); 
    timer->start(15); 
} 

タイマのmove()fuctionは定期的に実行されます。上記のreverseVelocityIfOutOfBounds()で、

void Ball::move(){ 
    reverseVelocityIfOutOfBounds(); 
    handlePaddleCollision(); 
    handleBlockCollision(); 
    moveBy(xVelocity,yVelocity); 
} 

このような条件があります。

// bottom edge - NONE (can fall through bottom) 
if (mapToScene(rect().topRight()).y() >= screenH){ 

これを実行するとQtimerを停止します。私はできる限りすべてを適用した。プログラムが予期せず終了することがあります。どのような助けが非常にappriciatedされますようにしてください。

+1

この質問は[最小限の完全で検証可能な例](https://stackoverflow.com/help/mcve)がなければ答えることができません。 – MrEricSir

答えて

1

あなたができる最も簡単なことは、タイマーをBallクラスのメンバー変数にすることです。次に、あなたはそれを扱い、reverseVelocityIfOutOfBounds()のtimer-> stop()を呼び出すことができます。

もう1つのことは、移動時にsender()を呼び出してタイマーオブジェクトのハンドルを取得し、それをreverseVelocityIfOutOfBounds()(不適切なスタイル)に渡し、スレッド間では機能しないことです。

関連する問題