Qt在任务栏图标和系统托盘图标上显示红点

在任务栏图标上显示红点

关键类:QWinTaskbarButton

cpp 复制代码
#include <QWinTaskbarButton>

QPointer<QWinTaskbarButton> taskbarBtn = nullptr;
if (!taskbarBtn) {
    taskbarBtn = new QWinTaskbarButton(window);
    taskbarBtn->setWindow(window->windowHandle());
}

if (checked) {
    QPixmap pixmap(10, 10);
    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen); // 不绘制边框
    painter.setBrush(Qt::red);
    painter.drawEllipse(2, 2, 8, 8);

    QIcon icon(pixmap);

    taskbarBtn->setOverlayIcon(icon);
}
else {
    taskbarBtn->clearOverlayIcon();
}

UI可以参考OBS录制开启。checked为true显示红点,false不显示红点。

使用QWinTaskbarButton::setOverlayIcon设置覆盖图标

在系统托盘上显示红点

关键类:QSystemTrayIcon

cpp 复制代码
    if (visible) {
        QPixmap pixmap;
        if (!pixmap.load(":/res/logo.png")) {
            qDebug() << "Failed to load icon from resource file.";
            return;
        }
        pixmap = pixmap.scaled(16, 16);
        QPainter painter(&pixmap);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(Qt::NoPen); // 不绘制边框
        painter.setBrush(Qt::red);
        painter.drawEllipse(0, 8, 8, 8);
        QIcon icon(pixmap);
        setIcon(icon);
    }
    else {
        setIcon(QIcon(":/res/logo.png"));
    }

先将logo加载到QPixmap,再在QPixmap上用QPainter绘制红点。最后调用QSystemTrayIcon::setIcon

相关推荐
用户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
桥田智能16 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G16 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt