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);

运行效果:

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

相关推荐
19岁开始学习12 分钟前
关于php-fpm的调优和配置
开发语言·php
珊瑚里的鱼35 分钟前
第一讲 | 算法复杂度
c语言·开发语言·数据结构·笔记·算法·visualstudio·visual studio
m0_5557629039 分钟前
vscode 配置qt
ide·vscode·qt
悦悦子a啊2 小时前
C++之string
开发语言·数据结构·c++
赛卡2 小时前
IPOF方法学应用案例:动态电压频率调整(DVFS)在AIoT芯片中的应用
开发语言·人工智能·python·硬件工程·软件工程·系统工程·ipof
网络大镖客2 小时前
JavaScript高级进阶(五)
开发语言·前端·javascript
API小爬虫3 小时前
利用 Python 爬虫按关键字搜索 1688 商品详情:实战指南
开发语言·爬虫·python
不当菜虚困3 小时前
JAVA设计模式——(九)工厂模式
java·开发语言·设计模式
柴郡猫乐园3 小时前
智能指针之设计模式5
开发语言·设计模式·智能指针
IT技术员3 小时前
【Java学习】Java的CGLIB动态代理:通俗解释与使用指南
java·开发语言·学习