QTimer 在主函数之外不起作用

QTimer doesn't work outside main function

简而言之:这段代码位于主函数中,效果很好。此代码对两个图形元素执行交换。

        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()));

但是当我想将这段代码从 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, &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()));
    }

更新: 现在有效了:

    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()));
    }

您正在将定时器的 timeout() 信号连接到例如

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

它是堆栈上的一个对象,因此是函数的局部对象,并在函数完成时被删除。如果其中一个对象(发送方、接收方)被删除,Qt 将断开连接,因此当定时器结束时,没有任何连接到它的 timeout() 信号。