Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSystemTrayIcon selected / active icon
QtWS25 Last Chance

QSystemTrayIcon selected / active icon

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    isoiphone
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vezprog
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isoiphone
        wrote on last edited by
        #3

        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]);
        }
        }
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved