QT实现消息未读提示

cpp 复制代码
#ifndef BTNQLABELDEMO_H
#define BTNQLABELDEMO_H

#include <QWidget>

#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>

// 自定义带消息提示的按钮
class BadgeButton : public QPushButton
{
    Q_OBJECT
public:
    explicit BadgeButton(QWidget *parent = nullptr) : QPushButton(parent)
    {
        // 创建红色提示原点
        m_badge = new QLabel(this);
        m_badge->setFixedSize(10, 10);  // 原点大小
        m_badge->setStyleSheet("background-color: red; border-radius: 5px;");  // 圆形样式
        m_badge->hide();  // 默认隐藏

        // 设置原点位置(右上角)
        updateBadgePosition();
    }

    // 显示/隐藏提示原点
    void setBadgeVisible(bool visible)
    {
        m_badge->setVisible(visible);
    }

protected:
    // 按钮大小改变时更新原点位置
    void resizeEvent(QResizeEvent *event) override
    {
        QPushButton::resizeEvent(event);
        updateBadgePosition();
    }

private:
    QLabel *m_badge;

    // 更新原点位置到右上角
    void updateBadgePosition()
    {
        int x = width() - m_badge->width() - 2;  // 靠右偏移2px
        int y = 2;  // 靠上偏移2px
        m_badge->move(x, y);
    }
};

#endif // BTNQLABELDEMO_H
相关推荐
北i16 分钟前
Dubbo 默认超时重试:把下游打雪崩的隐形放大器
开发语言·qt·dubbo
扶尔魔ocy3 小时前
【qt openGL入门笔记】OSG实现显示飞机模型飞行的点迹(二)
qt·opengl·osg
Python图像识别5 小时前
43-【2027毕设】YOLO11暴力检测识别系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计
bug和崩溃我都要15 小时前
QT 开发的专业截图录屏工具 - macOS版 / Windows版
windows·qt·mac·录屏·截屏
Python图像识别18 小时前
40-【2027毕设】YOLO11面部口罩检测识别系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计
Python图像识别18 小时前
39-【2027毕设】YOLO11行人摔倒检测系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计
Quz1 天前
QML PathView:直线路径与曲线路径
qt·动画·qml
扶尔魔ocy1 天前
【qt openGL入门笔记】OSG实现飞机模型在网格图上飞行
qt·opengl·osg
西飘客2 天前
vs 怎么根据dll 生成lib文件
c++·qt