Qt Toast 一个淡出提示效果

1.效果

在这里插入图片描述
自适应大小

2.简述

静态方法实现,不用实例化。
自动居中父控件,父控件传nullptr在屏幕居中。

调用方式showHintLabel(nullptr,“设置错误”);
showHintLabel(nullptr,“设置错误,请设置有效的值,\n范围为20~100;”);

定义

      static void widgetCenter(QWidget *pWidget,QWidget*parent = nullptr);
      static void showHintLabel(QWidget *parent,QString strText,QString strFontSize = "12px",QString strColor = "#ffffff",QString strBgColor = "#000000"); //一个提示便签  会淡化消失
     

实现

void widgetCenter(QWidget *pWidget, QWidget *parent)
{
    QSize parentSize = (nullptr == parent) ? QApplication::desktop()->screenGeometry().size() : parent->size(); //双屏情况下在主屏幕上提示
    QSize subSize = parentSize - pWidget->size();

    pWidget->move(subSize.width()/2,subSize.height()/2);
}

void showHintLabel(QWidget *parent, QString strText, QString strFontSize, QString strColor, QString strBgColor)
{
    if(nullptr == parent){
        parent = QApplication::desktop()->screen();
    }

    QFrame *pFrmBg = new QFrame(parent); //为了兼容parent为nullptr时的圆角边框  方法是背景透明 上边叠加圆角控件

    QLabel *pHintLabel = new QLabel(pFrmBg);
    pHintLabel->setStyleSheet(QString("QLabel{background:%1;color:%2;font:%3 SimHei;border-radius:5px;}")
                              .arg(strBgColor).arg(strColor).arg(strFontSize));
    pHintLabel->setText(strText);
    pHintLabel->setAlignment(Qt::AlignCenter);
    pHintLabel->adjustSize();
    pHintLabel->resize(pHintLabel->size() + QSize(60,30));

    pFrmBg->resize(pHintLabel->size());
    pFrmBg->setWindowFlags(Qt::FramelessWindowHint);
    pFrmBg->setAttribute(Qt::WA_TranslucentBackground);
    CQuickTools::widgetCenter(pFrmBg,parent);
    pFrmBg->show();

    QPropertyAnimation *pAnimation = new QPropertyAnimation(pFrmBg,"windowOpacity");
    pAnimation->setDuration(2000);
    pAnimation->setEasingCurve(QEasingCurve::InCirc);
    pAnimation->setStartValue(1.0f);
    pAnimation->setEndValue(0.0f);
    pAnimation->start();
    connect(pAnimation,&QPropertyAnimation::finished,[=]{
        delete pAnimation;
        delete pFrmBg;
    });
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是唐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值