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

时间:2016-11-28 11:41:23

标签: c++ qt qtimer

简而言之:这段代码位于main函数中,这很有效。此代码在两个图形元素上执行交换。 enter image description here

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

1 个答案:

答案 0 :(得分:6)

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

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

是堆栈中的一个对象,因此是函数的本地对象,并在函数完成时删除。如果删除了其中一个对象(发送方,接收方),Qt将断开连接,因此在计时器结束的那一刻,没有任何与其timeout()信号相关的信息。

相关问题