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

运行效果:

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

相关推荐
用户805533698034 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner4 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz9 天前
QML Hello World 入门示例
qt
xcyxiner12 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner13 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner13 天前
DicomViewer (添加模型类)3
qt
xcyxiner14 天前
DicomViewer (目录调整) 2
qt
xcyxiner14 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00616 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术16 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript