如何检测QPropertyAnimation何时完成?

huangapple go评论80阅读模式
英文:

How to detect when QPropertyAnimation finishes?

问题

You can achieve this by connecting a slot to the finished signal of the QPropertyAnimation. This slot will be called when the animation finishes, allowing you to hide the text_button at the end of the animation.

Here's the modified code:

void testbutton::buttonPressEvent(bool pressed_arg) {
    QPropertyAnimation *animation = new QPropertyAnimation(m_button_bg, "size", this);
    animation->setDuration(500);
    animation->setEasingCurve(QEasingCurve::InCurve);
    int maxwidth = mwidth;
    if (pressed_arg == true) {
        animation->setStartValue(QSize(m_button_bg->width(), 39));
        animation->setEndValue(QSize(39, 39));
        pressed = false;
    } else {
        animation->setStartValue(QSize(m_button_bg->width(), 39));
        animation->setEndValue(QSize(maxwidth, 39));
        pressed = true;
        text_button->show();
    }

    // Connect the animation's finished signal to a slot
    connect(animation, &QPropertyAnimation::finished, this, &testbutton::animationFinished);

    // Start the animation
    animation->start();
}

void testbutton::animationFinished() {
    // This slot will be called when the animation finishes
    // Hide the text_button at the end of the animation
    text_button->hide();
}

By connecting the finished signal of the animation to the animationFinished slot, you ensure that text_button->hide() is called at the end of the animation.

英文:

I have a code:

void testbutton::buttonPressEvent(bool pressed_arg){
    QPropertyAnimation *animation = new QPropertyAnimation(m_button_bg, "size", this);
    animation->setDuration(500);
    animation->setEasingCurve(QEasingCurve::InCurve);
    int maxwidth = mwidth;
    if(pressed_arg == true)
        {
            animation->setStartValue(QSize(m_button_bg->width(), 39));
            animation->setEndValue(QSize(39, 39));
            pressed = false;
            animation->start();


         }
    else{
        animation->setStartValue(QSize(m_button_bg->width(), 39));
        animation->setEndValue(QSize(maxwidth, 39));
        pressed = true;
        text_button->show();
        animation->start();

    }
}

I want the text_button to be hidden after the end of the animation from the pressed_arg == true option, because the animation itself cannot do this due to the fact that this button contains text and cannot be compressed to zero, so I want to track the end of the animation and then apply text_button->hide().

How can I do that?

P.S: the animation compresses the frame in which this button is located.

I tried:

animation->start();
text_button->hide();

but the button disappeared right when the animation started, and it doesn't look like I expected it.

答案1

得分: 0

QPropertyAnimation 继承自 QAbstractAnimation,它提供了一个 finished 信号,将其连接到你的 text_buttonhide 方法。

connect(animation, &QPropertyAnimation::finished, text_button, &QPushButton::hide );

有关更多信息,请参阅此处

英文:

QPropertyAnimation inherits QAbstractAnimation which provides a finished signal, connect it to a your text_button's hide method.

connect(animation, &QPropertyAnimation::finished, text_button, &QPushButton::hide );

see this for more information.

huangapple
  • 本文由 发表于 2023年4月11日 04:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定