qt轻弹窗设计(自动隐藏)

1、显示效果
在这里插入图片描述

2、实现方法
使用QWidget(或QFrame) + QLabel 作为弹窗的主体和内容,再使用QPropertyAnimation实现动画效果及其信号槽自动释放窗口。

3、QPropertyAnimation说明
QPropertyAnimation类定义了Qt的属性动画。QPropertyAnimation以Qt属性做差值,作为属性值存储在QVariants中,该类继承自QVariantAnimation,并支持基类相同的元类型动画。

4、实现代码

void showHintLabel(QWidget *parent,
                          QString strText, int time = 2000,
                          QString strFontSize = "16px",
                          QString strColor = "#ffffff",
                          QString strBgColor = "#202020");

void MainWindows::showHintLabel(QWidget *parentWin, QString strText,  int time, QString strFontSize, QString strColor, QString strBgColor)
{
    if(nullptr == parentWin)
        parentWin = QApplication::desktop()->screen();

    QWidget *widgetPtr = new QWidget();  //为了兼容parent为nullptr时的圆角边框  方法是背景透明 上边叠加圆角控件
    widgetPtr->setWindowFlags(Qt::FramelessWindowHint);    //去除外框
    widgetPtr->setAttribute(Qt::WA_TranslucentBackground); //设置窗体背景透明

    QLabel *pLabel = new QLabel(widgetPtr);    //创建显示标签
    pLabel->setAlignment(Qt::AlignCenter);  //文字居中显示
    pLabel->setStyleSheet(QString("QLabel{background:%1;color:%2;font:%3 SimHei;border-radius:5px;}")
                              .arg(strBgColor).arg(strColor).arg(strFontSize)); //社长标签风格
    pLabel->setText(strText);                   //设置文字内容
    pLabel->adjustSize();
    pLabel->resize(pLabel->size() + QSize(120,120));
    //设置pFrame大小和位置
    widgetPtr->resize(pLabel->size()); //设置大小为label大小
    widgetPtr->move((parentWin->width()-widgetPtr->width())/2   + parentWin->pos().x(),
                 (parentWin->height()-widgetPtr->height())/2 + parentWin->pos().y());
    widgetPtr->show();

    //创建动画,使窗口淡化消失
    QPropertyAnimation *pAnimation = new QPropertyAnimation(widgetPtr,"windowOpacity",this);
    pAnimation->setDuration(time);
    pAnimation->setEasingCurve(QEasingCurve::InCirc);
    pAnimation->setStartValue(1.0);
    pAnimation->setEndValue(0.0);
    pAnimation->start();        //启动动画
    //动画结束后删除创建的资源
    connect(pAnimation,&QPropertyAnimation::finished,[=]{
        delete pAnimation;
        delete widgetPtr;
    });
}

其他推荐内容:

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值