2016-11-28 106 views
0

簡而言之:這段代碼位於主函數中,並且工作得很好。此代碼在兩個圖形元素上執行交換。 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會斷開連接,所以定時器結束的時候沒有任何連接到它的timeout()信號。

相關問題