英文:
How to change text color of QLabel using QAnimation?
问题
我有一个带有一些文本的QLabel
。我想创建一个动画,将文本颜色更改为红色,持续1秒,然后将其更改回初始值。
我在我的代码中所做的是:
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(myLabel);
myLabel->setGraphicsEffect(effect);
QPropertyAnimation *declineAnimation = new QPropertyAnimation(effect, "color");
declineAnimation->setDuration(1000);
declineAnimation->setStartValue(QColor(252, 40, 3));
declineAnimation->setEndValue(QColor(254, 220, 105));
declineAnimation->start();
英文:
I have QLabel
with some text set on it. I want to create animation which changes text color to red for 1 second and than changes it back to beginning value.
What I do in my code is:
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(myLabel);
myLabel -> setGraphicsEffect(effect);
QPropertyAnimation *declineAnimation = new QPropertyAnimation(effect, "color");
declineAnimation->setDuration(1000);
declineAnimation->setStartValue(QColor(252, 40, 3));
declineAnimation->setEndValue(QColor(254,220,105));
declineAnimation->start();
答案1
得分: 1
只要你改变颜色,但你试图做的只是在动画完成后回退。
QLabel* myLabel = new QLabel("颜色化文本");
QGraphicsColorizeEffect* effect = new QGraphicsColorizeEffect(myLabel);
myLabel->setGraphicsEffect(effect);
QPropertyAnimation* declineAnimation = new QPropertyAnimation(effect, "color");
declineAnimation->setDuration(1000);
declineAnimation->setStartValue(QColor(252, 40, 3));
declineAnimation->setEndValue(QColor(254, 220, 105));
declineAnimation->start(QAbstractAnimation::KeepWhenStopped);
QObject::connect(declineAnimation, &QAbstractAnimation::finished, declineAnimation, [declineAnimation]
{
if (declineAnimation->direction() == QAbstractAnimation::Forward)
declineAnimation->setDirection(QAbstractAnimation::Backward);
else
declineAnimation->setDirection(QAbstractAnimation::Forward);
declineAnimation->start(QAbstractAnimation::KeepWhenStopped);
}
);
这将创建一个无限循环的颜色变化,前后交替。你所要求的只是一个发生。你可以更改连接中的 Lambda 表达式以实现这一目标:
QObject::connect(declineAnimation, &QAbstractAnimation::finished, declineAnimation, [declineAnimation]
{
declineAnimation->setDirection(QAbstractAnimation::Backward);
declineAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}, Qt::QueuedConnection
);
注意:与前一种情况不同,这里的连接需要是一个排队连接。
英文:
Up to you to change the color but what you are trying to do simply looks like reverting the animation after it completes.
QLabel* myLabel = new QLabel("Colorized text");
QGraphicsColorizeEffect* effect = new QGraphicsColorizeEffect(myLabel);
myLabel->setGraphicsEffect(effect);
QPropertyAnimation* declineAnimation = new QPropertyAnimation(effect, "color");
declineAnimation->setDuration(1000);
declineAnimation->setStartValue(QColor(252, 40, 3));
declineAnimation->setEndValue(QColor(254, 220, 105));
declineAnimation->start(QAbstractAnimation::KeepWhenStopped);
QObject::connect(declineAnimation, &QAbstractAnimation::finished, declineAnimation, [declineAnimation]
{
if (declineAnimation->direction() == QAbstractAnimation::Forward)
declineAnimation->setDirection(QAbstractAnimation::Backward);
else
declineAnimation->setDirection(QAbstractAnimation::Forward);
declineAnimation->start(QAbstractAnimation::KeepWhenStopped);
}
);
This creates an infinite loop of changing color, back and forth.
What you asked was only 1 occurrence. You can change the lambda in the connect to achieve that:
QObject::connect(declineAnimation, &QAbstractAnimation::finished, declineAnimation, [declineAnimation]
{
declineAnimation->setDirection(QAbstractAnimation::Backward);
declineAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}, Qt::QueuedConnection
);
Note: unlike the former case, the connection needs to be a queued connection here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论