Garbage pixels in transparent background of QSystemTrayIcon

136 Views Asked by At

I'm trying to make a system tray icon using Qt 5.10.0. The icon would be non-rectangular (text actually). My current code works normally on a KDE Plasma panel, but on XFCE4-panel it appears to have garbage in the background, whenever icon's pixel is transparent. The garbage typically represents pieces of some of the already present icons in the system tray, but sometimes has some pieces of other windows.

Almost all other applications' icons look clean, including Qt-based apps like QBittorrent, Klipper, KTorrent, as well as GTK-based (Pidgin). The only exception is Dropbox and my code. Notable difference is that my code, as well as Dropbox, are both Qt5-based, while the above mentioned correctly-looking Qt applications are Qt4-based. Compiling my code for Qt4 indeed doesn't show the problem.

Below is the code. What am I doing wrong here?

#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"
0

There are 0 best solutions below