#include "tray.h"Tray::Tray(QWidget *parent) : QWidget(parent){ createActions(); createTrayIcon(); setIcon(); trayIcon->show();}void Tray::createActions(){ open = new QAction(tr("&Open"), this); connect(open, SIGNAL(triggered()), this, SLOT(show())); close = new QAction(tr("&Quit"), this); connect(close, SIGNAL(triggered()), qApp, SLOT(quit()));}void Tray::createTrayIcon(){ trayIconMenu = new QMenu(this); trayIconMenu->addAction(open); trayIconMenu->addSeparator(); trayIconMenu->addAction(close); trayIcon = new QSystemTrayIcon(this); trayIcon->setContextMenu(trayIconMenu); connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));}void Tray::setIcon(){ trayIcon->setIcon(QIcon(":/tray.ico"));}void Tray::trayIconClicked(QSystemTrayIcon::ActivationReason reason){ if(reason == QSystemTrayIcon::Trigger) this->show();}void Tray::closeEvent(QCloseEvent *event){ if (trayIcon->isVisible()) { trayIcon->showMessage(tr("Still here!!!"), tr("This application is still running. To quit please click this icon and select Quit")); hide(); event->ignore(); }}Tray::~Tray(){ delete trayIcon; delete trayIconMenu; delete open; delete close;}
#ifndef TRAY_H#define TRAY_H#include <QtGui>#include <QWidget>#include <QSystemTrayIcon>#include <QCloseEvent>class QMenu;class Tray : public QWidget{ Q_OBJECTpublic: explicit Tray(QWidget *parent = 0); ~Tray();private: void createActions(); void createTrayIcon(); void setIcon(); void closeEvent(QCloseEvent *); QSystemTrayIcon *trayIcon; QMenu *trayIconMenu; QAction *open; QAction *close;private slots: void trayIconClicked(QSystemTrayIcon::ActivationReason);};#endif // TRAY_H
#include "tray.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); if (!QSystemTrayIcon::isSystemTrayAvailable()) { QMessageBox::critical(0, QObject::tr("Tray"), QObject::tr("I couldn't detect any system tray " "on this system.")); return 1; } QApplication::setQuitOnLastWindowClosed(false); Tray w; w.show(); return a.exec();}
Tray w;w.show();
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; if(onlyTray) w.showTray(); else w.show(); return a.exec();}
#include "ui_mainwindow.h"class QSystemTrayIcon;class QAction;class QMenu;class MainWindow : public QMainWindow, private Ui::MainWindow{ Q_OBJECTprivate: QSystemTrayIcon *trayIcon; QAction *quitAction; QMenu *trayIconMenu;public: explicit MainWindow(QWidget *parent = 0); void showTray();};const bool onlyTray=true;
#include "mainwindow.h"#include <QSystemTrayIcon>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ setupUi(this);}void MainWindow::showTray(){ // создание иконки trayIconMenu = new QMenu(this); quitAction = new QAction(tr("Выход"), this); connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); trayIconMenu->addAction(quitAction); trayIcon = new QSystemTrayIcon(this); trayIcon->setContextMenu(trayIconMenu); trayIcon->setIcon(QIcon(":/blueflag.png")); trayIcon->setToolTip(this->windowTitle()); trayIcon->show();}
QSystemTrayIcon *tray;QMenu *trayMenu;
QSystemTrayIcon tray;QMenu trayMenu;
QMenu trayMenu;trayMenu.addSeparator();
QMenu *trayMenu;trayMenu = new Qmenu;trayMenu->addSeparator();