QT实现弹出提示框缓慢变淡消失

参照原文:qt 消息弹出框 ,无框,缓慢自动消失

在根据原文作者的基础上对代码进行了修改,修复了提示框水平方向不居中,使用时受父类影响致颜色修改失效,定时器未销毁等bug,以及按照自己的编码习惯进行了重新编码

适用版本:Qt5

效果演示:

 主窗体调用:

void Widget::on_pushButton_clicked()
{
    MessageTips *mTips=new MessageTips("我藏在人群中然后失去晴空");
    mTips->show();
}

这里和原文调用方式不同,不再继承this(否则会受到父类的样式表影响导致修改颜色等操作失败),而是在MessageTips的构造函数中设置关闭窗口自动调用析构函数

messagetips.h

#ifndef MESSAGETIPS_H
#define MESSAGETIPS_H

#include <QWidget>

class QHBoxLayout;
class QLabel;
class MessageTips : public QWidget
{
    Q_OBJECT
public:
    explicit MessageTips(QString showStr="默认显示", QWidget *parent = nullptr);
    ~MessageTips();
    //设置背景颜色
    void setBackgroundColor(const QColor &value);
    //设置文本颜色
    void setTextColor(const QColor &value);
    //设置文本字体大小
    void setTextSize(int value);
    //设置显示时间
    void setShowTime(int value);
    //设置关闭速度
    void setCloseTimeSpeed(int closeTime = 100,double closeSpeed = 0.1);
    //设置边框颜色
    void setFrameColor(const QColor &value);
    //设置边框粗细
    void setFrameSize(int value);
    //设置透明度
    void setOpacityValue(double value);

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    void InitLayout();//初始化窗体的布局和部件
    QHBoxLayout *hBoxlayout;//布局显示控件布局
    QLabel *mText;//用于显示文字的控件
    QString showStr;//显示的字符串

    QColor backgroundColor;//窗体的背景色
    QColor textColor;//字体颜色
    int textSize;//显示字体大小
    int showTime;//显示时间
    int closeTime;//关闭需要时间
    double closeSpeed;//窗体消失的平滑度,大小0~1
    QColor frameColor;//边框颜色
    int frameSize;//边框粗细大小
    double opacityValue;//窗体初始化透明度
};

#endif // MESSAGETIPS_H

messagetips.cpp

#include "messagetips.h"

#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QTimer>
#include<QApplication>
#include<QDebug>

MessageTips::MessageTips(QString showStr,QWidget *parent) : QWidget(parent),
    hBoxlayout(new QHBoxLayout(this)),
    mText(new QLabel(this)),
    backgroundColor(QColor(120,120,120)),
    textColor(QColor(255,255,255)),
    textSize(14),
    showTime(1000),
    closeTime(100),
    closeSpeed(0.1),
    frameColor(QColor(0,0,0,0)),
    frameSize(0),
    opacityValue(1.0)
{
    setWindowFlags(Qt::Window|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::Tool|Qt::X11BypassWindowManagerHint);
    this->setAttribute(Qt::WA_TranslucentBackground); // ****这里很重要****
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件
    this->setAttribute(Qt::WA_DeleteOnClose,true);//关闭窗口自动调用析构函数
    this->showStr = showStr;
    hBoxlayout->addWidget(mText);
    InitLayout();
}

MessageTips::~MessageTips()
{
    if (hBoxlayout != NULL)
    {
        delete hBoxlayout;
        hBoxlayout = NULL;
    }
    if (mText != NULL)
    {
        delete mText;
        mText = NULL;
    }
}

void MessageTips::InitLayout()
{
    this->setWindowOpacity(opacityValue);

    //文字显示居中,设置字体,大小,颜色
    QFont font = QFont("微软雅黑",textSize,QFont::Bold);
    mText->setStyleSheet("QLabel{background-color:transparent;}");
    mText->setFont(font);
    mText->setText(showStr);
    mText->adjustSize();
    mText->setAlignment(Qt::AlignCenter);
    QPalette label_pe;//设置字体颜色
    label_pe.setColor(QPalette::WindowText, textColor);
    mText->setPalette(label_pe);

    QTimer *mtimer = new QTimer(this);//隐藏的定时器
    mtimer->setTimerType(Qt::PreciseTimer);
    connect(mtimer,&QTimer::timeout,this,[=](){
        if(opacityValue<=0){ this->close(); }
        opacityValue = opacityValue-closeSpeed;
        this->setWindowOpacity(opacityValue);    //设置窗口透明度
        });

    QTimer *mShowtimer = new QTimer(this);//显示时间的定时器
    mShowtimer->setTimerType(Qt::PreciseTimer);// 修改定时器对象的精度
    connect(mShowtimer,&QTimer::timeout,this,[=](){
        mtimer->start(closeTime);//执行延时自动关闭
        mtimer->destroyed();
        mShowtimer->deleteLater();
        });
    mShowtimer->start(showTime);

    //设置屏幕居中显示
    QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以
    this->move((desktop->width() - mText->width())/2, (desktop->height() - mText->height())*7/8);
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件
}

void MessageTips::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setBrush(QBrush(backgroundColor));//窗体的背景色

    painter.setPen(QPen(frameColor,frameSize));//窗体边框的颜色和笔画大小
    QRectF rect(0, 0, this->width(), this->height());
    painter.drawRoundedRect(rect, 15, 15); // round rect
}

void MessageTips::setBackgroundColor(const QColor &value)
{
    backgroundColor = value;
}

void MessageTips::setTextColor(const QColor &value)
{
    textColor = value;
    InitLayout();
}

void MessageTips::setTextSize(int value)
{
    textSize = value;
    InitLayout();
}

void MessageTips::setShowTime(int value)
{
    showTime = value;
    InitLayout();
}

void MessageTips::setCloseTimeSpeed(int closeTime, double closeSpeed)
{
    if(closeSpeed>0 && closeSpeed<=1){
       this->closeSpeed = closeSpeed;
    }
   this->closeTime = closeTime;
    InitLayout();
}

void MessageTips::setFrameColor(const QColor &value)
{
    frameColor = value;
}

void MessageTips::setFrameSize(int value)
{
    frameSize = value;
}

void MessageTips::setOpacityValue(double value)
{
    opacityValue = value;
    InitLayout();
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过使用Qt的动画框架来实现对话框的渐变缓慢弹出效果。具体步骤如下: 1. 创建一个QGraphicsOpacityEffect对象,用于设置对话框的不透明度。 2. 将QGraphicsOpacityEffect对象应用到对话框上。 3. 创建一个QPropertyAnimation对象,用于控制QGraphicsOpacityEffect对象的不透明度属性。 4. 设置QPropertyAnimation对象的属性范围、持续时间和缓动曲线。 5. 启动QPropertyAnimation对象,观察对话框的渐变缓慢弹出效果。 下面是一个示例代码,演示如何实现对话框的渐变缓慢弹出效果: ```cpp #include <QDialog> #include <QGraphicsOpacityEffect> #include <QPropertyAnimation> void showFadeDialog(QDialog *dialog) { // 创建QGraphicsOpacityEffect对象 QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(dialog); dialog->setGraphicsEffect(opacityEffect); // 创建QPropertyAnimation对象 QPropertyAnimation *animation = new QPropertyAnimation(opacityEffect, "opacity"); animation->setDuration(1000); // 设置持续时间为1秒 animation->setStartValue(0); // 设置起始值为0 animation->setEndValue(1); // 设置结束值为1 animation->setEasingCurve(QEasingCurve::OutQuad); // 设置缓动曲线 // 启动QPropertyAnimation对象 dialog->show(); animation->start(QAbstractAnimation::DeleteWhenStopped); } ``` 使用示例: ```cpp QDialog *dialog = new QDialog(); // 设置对话框的属性 showFadeDialog(dialog); // 显示对话框,带有渐变缓慢弹出效果 ``` 注意:此方法仅适用于Qt 5及以上版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值