I want to animate error messages inside the status bar of my application.
I use a QLabel for the statusbar.
On status bar change i do the following:
void sectiona::statusBarChanged(const QString& text, bool message)
{
oldStatusBarText=b->text();
if(message==false)
{
b->setText(text);
return;
}else //animated message
{
newtext=text;
newtext=newtext.prepend("<font color=\"red\"><b>").append("</b></font>");
b->setText(newtext);
QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect(b);
b->setGraphicsEffect(effect);
QPropertyAnimation* animation=new QPropertyAnimation(effect,"opacity");
effect->setOpacity(0.0);
animation->setDuration(3000);
animation->setKeyValueAt(0.0,1.0);
animation->setKeyValueAt(0.9,1.0);
animation->setKeyValueAt(1.0,0.0);
animation->setEasingCurve(QEasingCurve::InOutCubic);
connect(animation,SIGNAL(finished()),this,SLOT(statusBarAnimationFinished()));
animation->start(QAbstractAnimation::DeleteWhenStopped);
};
}
void sectiona::statusBarChanged(const QString& text, bool message)
{
QLabel* b=m_gui->m_textOut;
QString newtext;
oldStatusBarText=b->text();
if(message==false)
{
b->setText(text);
return;
}else //animated message
{
newtext=text;
newtext=newtext.prepend("<font color=\"red\"><b>").append("</b></font>");
b->setText(newtext);
QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect(b);
b->setGraphicsEffect(effect);
QPropertyAnimation* animation=new QPropertyAnimation(effect,"opacity");
effect->setOpacity(0.0);
animation->setDuration(3000);
animation->setKeyValueAt(0.0,1.0);
animation->setKeyValueAt(0.9,1.0);
animation->setKeyValueAt(1.0,0.0);
animation->setEasingCurve(QEasingCurve::InOutCubic);
connect(animation,SIGNAL(finished()),this,SLOT(statusBarAnimationFinished()));
animation->start(QAbstractAnimation::DeleteWhenStopped);
};
}
To copy to clipboard, switch view to plain text mode
if the new status bar is a temporary message I animate the fadeout for 3seconds and then on the finish of the animation I restore the permanent status bar text:
void sectiona::statusBarAnimationFinished()
{
QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect(b);
effect->setOpacity(0.0);
qDebug()<<effect->opacity();
b->setText(oldStatusBarText);
qDebug()<<effect->opacity();
b->setGraphicsEffect(effect);
qDebug()<<effect->opacity();
b->setText(oldStatusBarText);
qDebug()<<effect->opacity();
effect->setOpacity(1.0);
qDebug()<<effect->opacity();
}
void sectiona::statusBarAnimationFinished()
{
QLabel* b=m_gui->m_textOut;
QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect(b);
effect->setOpacity(0.0);
qDebug()<<effect->opacity();
b->setText(oldStatusBarText);
qDebug()<<effect->opacity();
b->setGraphicsEffect(effect);
qDebug()<<effect->opacity();
b->setText(oldStatusBarText);
qDebug()<<effect->opacity();
effect->setOpacity(1.0);
qDebug()<<effect->opacity();
}
To copy to clipboard, switch view to plain text mode
It works fine except a minor detail. The qDebug statements return:
0
0
0
0
1
0
0
0
0
1
To copy to clipboard, switch view to plain text mode
, so the opacity changes only on the final statement, <b>BUT</b> the previous text blinks for a frame before setting the oldStatusBarText
I want to smoothly disappear the message, and the just update the QLabel with the previous text.
Any ideas?
Bookmarks