在任务栏图标上显示红点
关键类:QWinTaskbarButton
#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
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