我正在尝试使用 Qt 5.10.0 制作系统托盘图标。该图标将是非矩形的(实际上是文本)。我当前的代码在 KDE Plasma 面板上正常工作,但在 XFCE4 面板上,只要图标的像素是透明的,它的背景就会出现垃圾。垃圾通常代表系统托盘中一些已经存在的图标的碎片,但有时还有一些其他窗口的碎片。
几乎所有其他应用程序的图标看起来都很干净,包括基于 Qt 的应用程序,如 QBittorrent、Klipper、KTorrent,以及基于 GTK 的 (Pidgin)。唯一的例外是 Dropbox 和我的代码。显着的区别是我的代码和 Dropbox 都是基于 Qt5 的,而上面提到的看起来正确的 Qt 应用程序是基于 Qt4 的。为 Qt4 编译我的代码确实没有显示问题。
下面是代码。我在这里做错了什么?
#include <QTimer>
#include <QPixmap>
#include <QPainter>
#include <QApplication>
#include <QSystemTrayIcon>
class MyTrayIcon : public QSystemTrayIcon
{
Q_OBJECT
QTimer updateTimer;
public:
MyTrayIcon()
{
connect(&updateTimer, SIGNAL(timeout()), this, SLOT(updateIcon()));
updateTimer.start(2*1000);
updateIcon();
}
private:
Q_SLOT void updateIcon()
{
const int size=22;
QPixmap pixmap(size,size);
// Make sure there's no garbage in the pixmap
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setPen(QColor(0,96,192));
painter.drawText(pixmap.rect(), Qt::AlignCenter, "5");
setIcon(pixmap);
}
};
int main(int argc, char** argv)
{
QApplication app(argc,argv);
MyTrayIcon trayIcon;
trayIcon.show();
return app.exec();
}
#include "temps.moc"