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
相关推荐
水煎包V:YEDIYYDS8884 小时前
QT QML 实现的旋钮按钮,类似收音机音量旋钮,可指示方向和角度
qt·qml·旋钮组件
龚建波4 小时前
Qt程序设计:QPromise/QFuture多线程任务管理
qt·qfuturewatcher·qpromise·qfuture
追烽少年x4 小时前
Qt面试题合集(三)
qt
FirstFrost --sy5 小时前
Qt控件美化:LineEdit与CheckBox实战
开发语言·qt
Larry_Yanan13 小时前
Qt多进程(一)进程间通信概括
开发语言·c++·qt·学习
小灰灰搞电子17 小时前
Qt 开发环境选择Qt Creator、Visual Studio还是 VS Code?
开发语言·qt·visual studio
淼淼76320 小时前
Qt调度 程序
开发语言·c++·windows·qt
明飞198720 小时前
QT笔记1
qt
林政硕(Cohen0415)20 小时前
ARM Qt 字体过小的问题
arm开发·qt