我正在编写一个图像查看器,它允许我执行一些操作。作为对某些操作(例如复制/移动/删除/..)的视觉反馈,我希望在应用程序窗口中间有一个像样的弹出窗口,告知已完成的操作,并在大约一秒钟后消失。
当然,我可以只使用一个小部件并修改它以满足我的需要:
..我只是想知道是否有专门用于此目的的东西
(我不是在谈论出现在窗口管理器的某些任务栏附近的托盘通知)
您可以使用qt中的动画效果实现漂亮的弹出淡入/淡出效果,示例代码如下:
QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect();
this->label->setGraphicsEffect(effect);
this->label->setStyleSheet("border: 3px solid gray;border-radius:20px;background-color:#ffffff;color:gray");
this->label->setAlignment(Qt::AlignCenter);
this->label->setText("Your Notification");
QPropertyAnimation* a=new QPropertyAnimation(effect,"opacity");
a->setDuration(1000); // in miliseconds
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
this->label->show();
connect(this->timer,&QTimer::timeout,this,&Notifier::fadeOut);
this->timer->start(2000); // 1000 ms to make the notification opacity full and 1000 seconds to call the fade out so total of 2000ms.
Run Code Online (Sandbox Code Playgroud)
和你的淡出方法为:
void fadeOut(){
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect();
this->label->setGraphicsEffect(effect);
QPropertyAnimation *a = new QPropertyAnimation(effect,"opacity");
a->setDuration(1000); // it will took 1000ms to face out
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(a,SIGNAL(finished()),this->label,SLOT(hide()));
}
Run Code Online (Sandbox Code Playgroud)