QTimer在主要功能之外不起作用

Vad*_*han 0 c++ qt qtimer

简而言之:这段代码位于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, &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()));
Run Code Online (Sandbox Code Playgroud)

但是当我想把这段代码从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()));
    }
Run Code Online (Sandbox Code Playgroud)

更新:现在它的工作原理:

    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()));
    }
Run Code Online (Sandbox Code Playgroud)

E4z*_*4z9 6

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

MyPointF centreSwap1(0, &positionBut1, &centre);
Run Code Online (Sandbox Code Playgroud)

它是堆栈中的一个对象,因此是函数的本地对象,并在函数完成时删除.如果其中一个对象(发送方,接收方)被删除,Qt将断开连接,因此在计时器完成的那一刻,没有任何东西连接到其timeout()信号.