qt 消息弹出框 ,无框,缓慢自动消失

本文介绍了如何使用Qt库创建一个无边框、可缓慢消失的消息提示框,包括设置显示时间、关闭时间和平滑度。通过QLabel显示消息,并利用两个定时器控制显示和关闭过程,同时实现屏幕中央定位。代码中还提供了设置背景色、边框色、字体大小和颜色等属性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:弹出一个无边框窗体,再添加一个QLabel显示消息,使用了两个定时器,一个控制显示时间,一个控制缓慢消失的时间(没过一个时间,把透明度-.0.1,当透明度=0,关闭窗体)
效果:
在这里插入图片描述
主窗体调用:

void MainWindow::on_pushButton_clicked()
{
    MessageTips *mMessageTips = new MessageTips("网络连接失败,正在重新连接中 . . .",this);
    mMessageTips->show();
}

把这个弹出框封装成一个类,可以直接调用:
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),
    opacityValue(1.0),
    textSize(18),
    textColor(QColor(255,255,255)),
    backgroundColor(QColor(192,192,192)),
    frameColor(QColor(211,211,211)),
    frameSize(2),
    showTime(3500),
    closeTime(100),
    closeSpeed(0.1),
    hBoxlayout(new QHBoxLayout(this)),
    mText(new QLabel(this))
{
    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);
    this->showStr = showStr;
    hBoxlayout->addWidget(mText);
    InitLayout();
}

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

    //文字显示居中,设置字体,大小,颜色
    font = new QFont("微软雅黑",textSize,QFont::Bold);
    mText->setFont(*font);
    mText->setText(showStr);
    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){
            mtimer->stop();
            this->close();
            return;
        }
        opacityValue = opacityValue-closeSpeed;
        this->setWindowOpacity(opacityValue);    //设置窗口透明度
    });

    QTimer::singleShot(showTime,[=](){mtimer->start(closeTime);});//执行延时自动关闭

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

void MessageTips::paintEvent(QPaintEvent *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
}




//设置关闭的时间和速度,speed大小限定0~1
void MessageTips::setCloseTimeSpeed(int closeTime, double closeSpeed)
{
    if(closeSpeed>0 && closeSpeed<=1){
        this->closeSpeed = closeSpeed;
    }
    this->closeTime = closeTime;
    InitLayout();
}


int MessageTips::getShowTime() const
{
    return showTime;
}

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

int MessageTips::getFrameSize() const
{
    return frameSize;
}

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

QColor MessageTips::getFrameColor() const
{
    return frameColor;
}

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

QColor MessageTips::getBackgroundColor() const
{
    return backgroundColor;
}

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

QColor MessageTips::getTextColor() const
{
    return textColor;
}

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

int MessageTips::getTextSize() const
{
    return textSize;
}

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

double MessageTips::getOpacityValue() const
{
    return opacityValue;
}

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



messagetips.h文件

#ifndef MESSAGETIPS_H
#define MESSAGETIPS_H

#include <QWidget>
# pragma execution_character_set("utf-8")

class QHBoxLayout;
class QLabel;
class MessageTips : public QWidget
{
    Q_OBJECT
public:
    explicit MessageTips(QString showStr="默认显示", QWidget *parent = nullptr);

    double getOpacityValue() const;
    void setOpacityValue(double value);

    int getTextSize() const;
    void setTextSize(int value);

    QColor getTextColor() const;
    void setTextColor(const QColor &value);

    QColor getBackgroundColor() const;
    void setBackgroundColor(const QColor &value);

    QColor getFrameColor() const;
    void setFrameColor(const QColor &value);

    int getFrameSize() const;
    void setFrameSize(int value);

    int getShowTime() const;
    void setShowTime(int value);

    void setCloseTimeSpeed(int closeTime = 100,double closeSpeed = 0.1);

protected:
    void paintEvent(QPaintEvent *event) override;

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

    double opacityValue;//窗体初始化透明度

    QFont *font;
    int     textSize;//显示字体大小
    QColor  textColor;//字体颜色

    QColor  backgroundColor;//窗体的背景色
    QColor  frameColor;//边框颜色
    int     frameSize;//边框粗细大小

    int     showTime;//显示时间

    int     closeTime;//关闭需要时间
    double  closeSpeed;//窗体消失的平滑度,大小0~1


signals:

};

#endif // MESSAGETIPS_H

不足之处,请告知

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

青鸟青史

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

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

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

打赏作者

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

抵扣说明:

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

余额充值