Qt5:显示通知弹出窗口

时间:2017-03-31 06:29:49

标签: qt notifications

我正在写一个图像查看器,它允许我做一些动作。作为对某些动作的视觉反馈(如复制/移动/删除/ ..),我希望在我的应用程序窗口中间有一个不错的弹出窗口,告知已完成的内容以及大约一秒后消失的内容。 / p>

当然我可以使用Widget并修改它以满足我的需求:

  • 放置在应用程序窗口的中间/顶部(无论布局如何)
  • 在给定时间后消失
  • 没有互动/关注可能 - 点击通知应该就像点击它背后的内容
  • 体面的风格(例如透明且易读)

..我只是想知道是否有专门用于此目的的东西

(我不是在谈论在窗口管理器的某个任务栏附近出现的托盘通知)

3 个答案:

答案 0 :(得分:2)

你可以在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.

和你的淡出方法:

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()));
}

答案 1 :(得分:0)

听起来你想使用QMessageBox。例如:

QMessageBox* msgbox = new QMessageBox(this);
msgbox->setWindowTitle("Note");
msgbox->setText("Successfully copied item foobar");
msgbox->open();

您可能希望根据需要更改modality并实施计时器以关闭对话框。

QTimer* timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), msgbox, SLOT(close()));
QObject::connect(timer, SIGNAL(timeout()), timer, SLOT(stop()));
QObject::connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater()));
timer->start(1000);

注意:示例代码,未经过测试。

答案 2 :(得分:0)

不知道你是在python还是C,但可能看看这个: http://doc.qt.io/qt-4.8/qmessagebox.html

然而,我会去一个新窗口(QWidget)并修改它。它只有几行,你可以通过Qtimer自动关闭。

相关问题