QSystemTrayIcon selected / active icon



  • Hello,

    I'm using a QSystemTrayIcon on OS/X for my app (QT 5.0.2)

    The standard behaviour for apps is to have the icon change when the menu bar icon is selected (usually to an inverted white icon). I can't seem to get this to work. Am I missing something?

    I've tried setting the QIcon::Selected and QIcon::Active modes of my icon to no effect.

    @
    icon.addFile(":/tray-online.png", QSize(), QIcon::Normal);
    icon.addFile(":/tray-online-selected.png", QSize(), QIcon::Selected);
    icon.addFile(":/tray-online-selected.png", QSize(), QIcon::Active);

    ...

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(icon);
    ...
    @
    (The QIcon::Normal icon is always displayed)

    Googling all I could turn up was a merge request from 2009 supposedly adding this behaviour.



  • You can get the event on which type of click event was on the item, once you have that, you can set the proper icon you want. Here's what I use when implementing a tray icon and using a menu:

    @
    // setup actions
    showAction = new QAction(tr("&Show"), this);
    connect(showAction, SIGNAL(triggered()), this, SLOT(showActionClicked()));
    configureAction = new QAction(tr("&Configure"), this);
    connect(configureAction, SIGNAL(triggered()), this, SLOT(configureActionClicked()));
    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), this, SLOT(quitActionClicked()));

    // create icon menu
    trayIconMenu = new QMenu(this);
    trayIconMenu->setWindowTitle("Quick Launch Menu");
    trayIconMenu->addAction(showAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(configureAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
    
    // create icon
    trayIcon = new QSystemTrayIcon(this);
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    trayIcon->setIcon(QIcon(":/images/images/mainicon.png"));
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->show();
    

    @

    your slot on click
    @
    /*
    // icon activated (private slot)
    */
    void SystemTrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason)
    {
    // check the activate reason
    switch (reason) {
    // double click
    case QSystemTrayIcon::DoubleClick: {
    trayIconMenu->popup(QCursor::pos());
    break;
    }
    // default...break
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::MiddleClick:
    default:
    break;
    }
    }
    @

    Do what you want on your click event.



  • I've hacked a workaround in place for now.
    It seems like a bug that the correct icon mode isn't used to draw. I'll open a ticket on this later.

    For now I'm just manually changing icon on events as suggested above.

    @
    void TrayWidget::createTrayIcon()
    {
    ..
    connect(trayIconMenu, &QMenu::aboutToHide, this, &TrayWidget::menuAboutToHide);
    connect(trayIconMenu, &QMenu::aboutToShow, this, &TrayWidget::menuAboutToShow);
    ..
    }

    void TrayWidget::menuAboutToShow()
    {
    if (trayIcon->icon().name() == icons[Icon_Online].name()) {
    trayIcon->setIcon(icons[Icon_Online_Selected]);
    }
    }

    void TrayWidget::menuAboutToHide()
    {
    if (trayIcon->icon().name() == icons[Icon_Online_Selected].name()) {
    trayIcon->setIcon(icons[Icon_Online]);
    }
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.