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

1、显示效果
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;
    });
}

其他推荐内容:

  • qt电池控件设计:https://blog.csdn.net/weixin_42887343/article/details/113932145
  • QWidget控件拖动:https://blog.csdn.net/weixin_42887343/article/details/114384324
  • QWidget控件旋转方法:https://blog.csdn.net/weixin_42887343/article/details/115037420
  • qt柱状图控件设计:https://blog.csdn.net/weixin_42887343/article/details/115124178
  • qt淡化提示框设计:https://blog.csdn.net/weixin_42887343/article/details/109303972
  • qt之led(点阵)控件类设计:https://blog.csdn.net/weixin_42887343/article/details/115348953

你可能感兴趣的:(C++\QT,qt,淡化窗体,轻弹窗,对话框,提示框)