Search code examples
qttrayicon

How to create a tray icon with custom image using QT?


I have tried to create a custom-painted icon using QT under window. The following code displays an icon, but it looks transparent instead of color filled :(. OS is WinXP SP3, IDE is latest QTCreator.

int main( int argc, char* argv[] )
{
  QApplication oApp( argc, argv );

  QImage oImg( 16, 16, QImage::Format_RGB32 );
  oImg.fill( qRgb( 255, 0, 255 ) );
  QPixmap oPixmap;
  oPixmap.fromImage( oImg, Qt::ColorOnly );
  QIcon oIcon( oPixmap );
  QSystemTrayIcon oTrayIcon( oIcon );
  oTrayIcon.show();

  return oApp.exec();
}

Solution

  • I couldn't figure out why, but if you save oImg to a file, you can see that the image is not filled. But if you fill QPixmap directly instead of oImg you can see the icon.

    int main( int argc, char* argv[] )
    {
        QApplication oApp( argc, argv );
    
        QPixmap oPixmap(16,16);
        oPixmap.fill(qRgb( 255, 0, 255 ));
    
        QIcon oIcon( oPixmap );
        QSystemTrayIcon oTrayIcon( oIcon );
        oTrayIcon.show();
    
        return oApp.exec();
    }