6: QT -- how to create QSystemTrayIcon

After create the menu, maybe we want to make it minimize to the tray.

So Here is the code.

1: XXXX.h

  private slots:

    void iconActivated(QSystemTrayIcon::ActivationReason reason);  

  private:
      QSystemTrayIcon *trayIcon;
      void createActions();
      void createTrayIcon();

2: XXXX.cpp

  XXXX()

  {

    ui->setupUi(this);
      QMessageBox::information(0,tr("Systray"),tr("Loaded."));
      createTrayIcon();
      connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated                (QSystemTrayIcon::ActivationReason)));
       trayIcon->show();

  }

  void XXXX::createTrayIcon()
  {
      trayIcon = new QSystemTrayIcon(this);
      trayIcon->setIcon(QIcon(":/images/TRANS.ICO"));
      trayIcon->setToolTip("Serverstatus");
      trayIcon->show();
  }
  void XXXX::iconActivated(QSystemTrayIcon::ActivationReason reason)
  {
      QMessageBox::information(0, tr("Systray"),tr("Testing"));
  }

3: Add resource to the project

  File --> New Projects --> QT(Resource Files) --> load the file in the image folder to the resource.

  Double click resource, Add --> Add Prefix --> change to /

  Add --> Add File --> go to image folder to add this file.

4: Right click the project name, then add existing files, find the image folder, add the files that used.

posted @ 2011-10-19 15:56  lcryby  阅读(234)  评论(0)    收藏  举报