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. try to add a context menu to QSystemTrayIcon

try to add a context menu to QSystemTrayIcon

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.4k 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    I use this code to make the QSystemTeayIcon and add a menu

      trayicon = new QSystemTrayIcon(this);
      trayicon->setIcon(QIcon(":/images/a.ico"));
      trayicon->setVisible(true);
      QMenu menue;
      menue.addAction("Exit",this,SLOT(close()));
      trayicon->setContextMenu(&menue);
      connect(trayicon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(Actived(QSystemTrayIcon::ActivationReason)));
    

    here the slot code of activation reason

    void MainWindow::Actived(QSystemTrayIcon::ActivationReason reason)
    {
      switch (reason) {
      case QSystemTrayIcon::DoubleClick:
      qDebug() << "Double click";
      break;
      case QSystemTrayIcon::Trigger:
      qDebug() << "Drag";
      break;
      default:
      break;
      }
    }
    
    

    what should i do to make menu appeare when i press right click on my app tray icon
    Thanks in advance

    1 Reply Last reply
    0
    • AmrCoderA Offline
      AmrCoderA Offline
      AmrCoder
      wrote on last edited by
      #2

      i solve it by add this to slot function

      void MainWindow::Actived(QSystemTrayIcon::ActivationReason reason)
      {
          switch (reason) {
          case QSystemTrayIcon::DoubleClick:
              this->show();
              break;
          case QSystemTrayIcon::Context:
              coustemContextMenu(QCursor::pos()); // here send position of the click
              break;
          default:
              break;
          }
      }
      

      here the coustemContextMenu function

      void MainWindow::coustemContextMenu(QPoint point)
      {
          QMenu menue;
          menue.setContextMenuPolicy(Qt::CustomContextMenu);
          menue.addAction("Exit",this,SLOT(close()));
          menue.exec(point); // to show menu on this point
      }
      
      1 Reply Last reply
      1

      • Login

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