[英]QTimer doesn't work outside main function
簡而言之:這段代碼位於main函數中,效果很好。 此代碼對兩個圖形元素執行交換。
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, ¢re);
button4->myPointF = ¢reSwap4;
QObject::connect(&timer, SIGNAL(timeout()), ¢reSwap4, SLOT(updateDownRight()));
QObject::connect(¢reSwap4, 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, ¢re);
button1->myPointF = ¢reSwap1;
QObject::connect(&timer, SIGNAL(timeout()), ¢reSwap1, SLOT(updateUpLeft()));
QObject::connect(¢reSwap1, SIGNAL(positionChanged()), button1, SLOT(slotMoveCircle()));
// ====================== MOVE 1 in POS of 4 ==========================
timer.start();
QTimer::singleShot(3000, Qt::PreciseTimer, &timer, SLOT(stop()));
但是,當我要將這段代碼從main中放到一個函數中時(為了縮短代碼並僅通過提供指向它們的指針來對元素執行交換),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, ¢re);
button4->myPointF = ¢reSwap4;
QObject::connect(&timer, SIGNAL(timeout()), ¢reSwap4, SLOT(updateDownRight()));
QObject::connect(¢reSwap4, 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, ¢re);
button1->myPointF = ¢reSwap1;
QObject::connect(&timer, SIGNAL(timeout()), ¢reSwap1, SLOT(updateUpLeft()));
QObject::connect(¢reSwap1, SIGNAL(positionChanged()), button1, SLOT(slotMoveCircle()));
// ====================== MOVE w1 in POS of w2 ==========================
timer.start();
qDebug() << timer.isActive();
QTimer::singleShot(3000, Qt::PreciseTimer, &timer, SLOT(stop()));
}
更新:現在它可以工作了:
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, ¢re);
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, ¢re);
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()));
}
您正在將計時器的timeout()信號連接到例如
MyPointF centreSwap1(0, &positionBut1, ¢re);
這是堆棧上的一個對象,因此是函數的本地對象,並且在函數完成時被刪除。 如果刪除了其中一個對象(發送者,接收者),則Qt斷開連接,因此計時器結束時,其timeout()信號沒有任何連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.