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

时间:2024-05-04 16:23:37

关键类:QSystemTrayIcon

    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
在这里插入图片描述