gpt4 book ai didi

c++ - QTimer 在主函数之外不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:33:48 29 4
gpt4 key购买 nike

简而言之:这段代码位于 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()));
}

最佳答案

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

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

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

关于c++ - QTimer 在主函数之外不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843457/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com