2016-11-28 17 views
0

要するに、このコードはメイン関数にあり、これはうまくいきます。このコードは、2つのグラフィック要素をスワップします。 enter image description hereQTimerはメイン関数の外では機能しません

 QTimer timer; 
     timer.setTimerType(Qt::PreciseTimer); 
     timer.setInterval(1000.0/30.0); 
     timer.setSingleShot(false); 

    // ====================== MOVE 4 in POS of 1 ========================== 
     QPointF centre(QLineF(button1->pos(), button4->pos()).pointAt(0.5)); 
     QPointF positionBut4 = button4->pos(); 

     MyPointF centreSwap4(0, &positionBut4, &centre); 
     button4->myPointF = &centreSwap4; 

     QObject::connect(&timer, SIGNAL(timeout()), &centreSwap4, SLOT(updateDownRight())); 
     QObject::connect(&centreSwap4, SIGNAL(positionChanged()), button4, SLOT(slotMoveCircle())); 

    // ====================== MOVE 4 in POS of 1 ========================== 

    // ====================== MOVE 1 in POS of 4 ========================== 
     QPointF positionBut1 = button1->pos(); 

     MyPointF centreSwap1(0, &positionBut1, &centre); 
     button1->myPointF = &centreSwap1; 

     QObject::connect(&timer, SIGNAL(timeout()), &centreSwap1, SLOT(updateUpLeft())); 
     QObject::connect(&centreSwap1, SIGNAL(positionChanged()), button1, SLOT(slotMoveCircle())); 
    // ====================== MOVE 1 in POS of 4 ========================== 
     timer.start(); 

     QTimer::singleShot(3000, Qt::PreciseTimer, &timer, SLOT(stop())); 

私が言う(QTimerが動作することを拒否(コードを短縮し、ちょうどそれらへのポインタを与えることによって要素にスワップを実行するために)関数にメインのうち、コードのこの部分を入れたいときしかし、それはです)アクティブしかし、タイムアウトがトリガーされていません。

void animateSwap(QGraphicsRectWidget *w1, QGraphicsRectWidget *w2, QTimer &timer) 
    { 
     QGraphicsRectWidget *button4, *button1; 
     if (w1->x() > w2->x()) 
     { 
       button4 = w2; 
       button1 = w1; 
     } 
     else 
     { 
      button4 = w1; 
      button1 = w2; 
     } 
     // ====================== MOVE w2 in POS of w1 ========================== 
     QPointF centre(QLineF(button1->pos(), button4->pos()).pointAt(0.5)); 
     QPointF positionBut4 = button4->pos(); 

     MyPointF centreSwap4(0, &positionBut4, &centre); 
     button4->myPointF = &centreSwap4; 

     QObject::connect(&timer, SIGNAL(timeout()), &centreSwap4, SLOT(updateDownRight())); 
     QObject::connect(&centreSwap4, SIGNAL(positionChanged()), button4, SLOT(slotMoveCircle())); 

     // ====================== !MOVE w2 in POS of w1 ========================== 

     // ====================== MOVE w1 in POS of w2 ========================== 
     QPointF positionBut1 = button1->pos(); 

     MyPointF centreSwap1(0, &positionBut1, &centre); 
     button1->myPointF = &centreSwap1; 

     QObject::connect(&timer, SIGNAL(timeout()), &centreSwap1, SLOT(updateUpLeft())); 
     QObject::connect(&centreSwap1, SIGNAL(positionChanged()), button1, SLOT(slotMoveCircle())); 
     // ====================== MOVE w1 in POS of w2 ========================== 
     timer.start(); 

     qDebug() << timer.isActive(); 

     QTimer::singleShot(3000, Qt::PreciseTimer, &timer, SLOT(stop())); 
    } 

UPDATE: 今では動作します:

void animateSwap(QGraphicsRectWidget *w1, QGraphicsRectWidget *w2, QTimer &timer, int cycles = 1) 
    { 
     QGraphicsRectWidget *button4, *button1; 
     if (w1->x() > w2->x()) 
     { 
       button4 = w2; 
       button1 = w1; 
     } 
     else 
     { 
      button4 = w1; 
      button1 = w2; 
     } 
     // ====================== MOVE w2 in POS of w1 ========================== 
     QPointF centre(QLineF(button1->pos(), button4->pos()).pointAt(0.5)); 
     QPointF positionBut4 = button4->pos(); 

     MyPointF *centreSwap4 = new MyPointF(0, &positionBut4, &centre); 
     button4->myPointF = centreSwap4; 

     QObject::connect(&timer, SIGNAL(timeout()), centreSwap4, SLOT(updateDownRight())); 
     QObject::connect(centreSwap4, SIGNAL(positionChanged()), button4, SLOT(slotMoveCircle())); 

     // ====================== !MOVE w2 in POS of w1 ========================== 

     // ====================== MOVE w1 in POS of w2 ========================== 
     QPointF positionBut1 = button1->pos(); 

     MyPointF *centreSwap1 = new MyPointF(0, &positionBut1, &centre); 
     button1->myPointF = centreSwap1; 

     QObject::connect(&timer, SIGNAL(timeout()), centreSwap1, SLOT(updateUpLeft())); 
     QObject::connect(centreSwap1, SIGNAL(positionChanged()), button1, SLOT(slotMoveCircle())); 
     // ====================== MOVE w1 in POS of w2 ========================== 
     timer.start(); 

     qDebug() << timer.isActive(); 

     QTimer::singleShot(3000 * cycles, Qt::PreciseTimer, &timer, SLOT(stop())); 
    } 
+0

その他のコードも追加できますか?メインの内容 – RvdK

+0

@RvdK確かに、 – Vadixem

+0

を教えてください。GraphicsViewクラスを作成するようにしてください:public QObject、public QGraphicsView –

答えて

6

あなたはタイムアウト()信号を接続していますタイマーを例えば

MyPointF centreSwap1(0, &positionBut1, &centre); 

スタック上のオブジェクトであり、したがって関数にローカルであり、関数が終了すると削除されます。 Qtはオブジェクト(送信者、受信者)の1つが削除された場合に接続を切断します。したがって、タイマーが終了すると、timeout()シグナルに何も接続されません。

関連する問題