英文:
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_button
的 hide
方法。
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论