QT全局气泡类型提示框【自动宽度、多屏自适应居中】

参考 qt 消息弹出框 ,无框,缓慢自动消失_new messagetips格式-CSDN博客 进行改进。

直接上代码。

头文件:

cpp 复制代码
#pragma once

#include <QObject>
#include <QWidget>

class MessageTips : public QWidget
{
    Q_OBJECT

public:
    MessageTips(QString showStr = "", QWidget* parent = nullptr);

    double getOpacityValue() { return m_opacity; };
    void setOpacityValue(double value) { m_opacity = value; };

    int getTextSize() { return m_textSize; };
    void setTextSize(int value) { m_textSize = value; };

    QColor getTextColor() { return m_textColor; };
    void setTextColor(const QColor& value) { m_textColor = value; };

    QColor getBackgroundColor() { return m_bgColor; };
    void setBackgroundColor(const QColor& value) { m_bgColor = value; };

    QColor getFrameColor() { return m_frameColor; };
    void setFrameColor(const QColor& value) { m_frameColor = value; };

    int getFrameSize() { return m_frameSize; };
    void setFrameSize(int value) { m_frameSize = value; };

    int getShowTime() { return m_showTime; };
    void setShowTime(int value) { m_showTime = value; };

    void setCloseTimeSpeed(int closeTime = 100, double closeSpeed = 0.1);
    void prepare();
    static void showMessageTips(QString showStr, QWidget* parent = nullptr);
protected:
    void paintEvent(QPaintEvent* event) override;

private:
    QString m_showStr;
    double m_opacity = 0.8;
    int     m_textSize = 18;//显示字体大小
    QColor  m_textColor = QColor(255, 255, 255);//字体颜色
    QColor  m_bgColor = QColor(192, 192, 192);//窗体的背景色
    QColor  m_frameColor = QColor(211, 211, 211);//边框颜色
    int     m_frameSize = 2;//边框粗细大小
    int     m_showTime = 3500;//显示时间
    int     m_closeTime = 100;//关闭需要时间
    double  m_closeSpeed = 0.1;//窗体消失的平滑度,大小0~1
};

cpp文件:

cpp 复制代码
#include "messagetips.h"
#include <QDesktopWidget>
#include <QPainter>
#include <QTimer>
#include <QApplication>
#include <QDebug>

MessageTips::MessageTips(QString showStr, QWidget* parent)
    : QWidget(parent),m_showStr(showStr)
{
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
    this->setAttribute(Qt::WA_TranslucentBackground); // ****这里很重要****
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedHeight(50);
    QFont font("Arial", m_textSize, QFont::Bold);
    QFontMetrics fontMetrics(font);
    int tw = fontMetrics.width(m_showStr);
    if (tw > 200)
        this->setFixedWidth(tw + 50);
    else
        this->setFixedWidth(200);
}

void MessageTips::prepare()
{
    this->setWindowOpacity(m_opacity);
    connect(this,SIGNAL(finished()),this,SLOT(onFinished()));
    QTimer* mtimer = new QTimer(this);//隐藏的定时器
    mtimer->setTimerType(Qt::PreciseTimer);
    connect(mtimer, &QTimer::timeout, this, [=]() {
        m_opacity = m_opacity - m_closeSpeed;
        if (m_opacity <= 0) {
            mtimer->stop();
            this->close();
            return;
        }
        else
            this->setWindowOpacity(m_opacity);    
        });

    //support for multi-screens.
    int scrIndex = 0;
	QDesktopWidget* desktop = QApplication::desktop();
	QWidget* acWgt = QApplication::activeWindow();
	if (acWgt)
	{
		QPoint checkPoint(acWgt->pos().x() + acWgt->width() / 2, acWgt->pos().y() + acWgt->height() / 2);
		for (int i = 0; i < desktop->screenCount(); i++)
		{
			if (desktop->screenGeometry(i).contains(checkPoint))
			{
				scrIndex = i;
				break;
			}
		}
	}
 
    QRect scrRect = desktop->screenGeometry(scrIndex);
   //设置屏幕居中显示
    this->move(scrRect.left() + (scrRect.width() - this->width()) / 2, 
        scrRect.top() + (scrRect.height() - this->height()) / 2);
    QTimer::singleShot(m_showTime, [=]() {mtimer->start(m_closeTime); });//执行延时自动关闭
}

void MessageTips::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setBrush(QBrush(m_bgColor));//窗体的背景色
    painter.setPen(QPen(m_frameColor, m_frameSize));//窗体边框的颜色和笔画大小
    QRectF rect(0, 0, this->width(), this->height());
    painter.drawRoundedRect(rect, 10, 10); // round rect
     //文字显示居中,设置字体,大小,颜色
    QFont font("Arial", m_textSize, QFont::Bold);
    painter.setFont(font);
    painter.setPen(QPen(m_textColor, m_frameSize));
    painter.drawText(rect,Qt::AlignHCenter | Qt::AlignVCenter,m_showStr);
}

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

void MessageTips::showMessageTips(QString showStr, QWidget* parent)
{
    MessageTips* mMessageTips = new MessageTips(showStr, parent);
    mMessageTips->setShowTime(1500);
    mMessageTips->prepare();
    mMessageTips->show();
}

调用方法:

cpp 复制代码
		MessageTips::showMessageTips("OK",this);

运行效果:

运行效果与参考链接类似。

相关推荐
春蕾夏荷_7282977251 小时前
qt ElaWidgetTools第一个实例
开发语言·qt
lizz312 小时前
从 JUnit 深入理解 Java 注解与反射机制
java·开发语言·junit
编啊编程啊程5 小时前
JUC之AQS
java·开发语言·jvm·c++·kafka
好学且牛逼的马9 小时前
GOLANG 接口
开发语言·golang
ahauedu9 小时前
AI资深 Java 研发专家系统解析Java 中常见的 Queue实现类
java·开发语言·中间件
韭菜钟9 小时前
在Qt中用cmake实现类似pri文件的功能
开发语言·qt·系统架构
闲人编程9 小时前
Python第三方库IPFS-API使用详解:构建去中心化应用的完整指南
开发语言·python·去中心化·内存·寻址·存储·ipfs
CTRA王大大10 小时前
【golang】制作linux环境+golang的Dockerfile | 如何下载golang镜像源
linux·开发语言·docker·golang
zhangfeng113311 小时前
以下是基于图论的归一化切割(Normalized Cut)图像分割工具的完整实现,结合Tkinter界面设计及Python代码示
开发语言·python·图论
还梦呦12 小时前
2025年09月计算机二级Java选择题每日一练——第五期
java·开发语言·计算机二级