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
相关推荐
Quz2 天前
QML Hello World 入门示例
qt
xcyxiner5 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner6 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner6 天前
DicomViewer (添加模型类)3
qt
xcyxiner7 天前
DicomViewer (目录调整) 2
qt
xcyxiner7 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能9 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G9 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G9 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G9 天前
71、打包发布---------打包发布
c++·qt