Qt4.8.2トレイ


widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <QSystemTrayIcon>

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = 0);
    ~Widget();

private:
    QSystemTrayIcon *trayIcon;

    QMenu * CreateTrayMenu();

private slots:
    void iconActived(QSystemTrayIcon::ActivationReason);

};

#endif // WIDGET_H

widget.cpp
#include "widget.h"
#include <QMenu>
#include <QAction>
#include <QApplication>
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    trayIcon=new QSystemTrayIcon(QIcon(":/img/tray.png"),this);
    // 
    trayIcon->setToolTip(tr(" Qt "));
    // 
    trayIcon->setContextMenu(CreateTrayMenu());
    // 
    trayIcon->setVisible(true);
    // 
    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActived(QSystemTrayIcon::ActivationReason)));
}

Widget::~Widget()
{
    
}
QMenu *Widget::CreateTrayMenu()
{
    QMenu *menu=new QMenu;
    QAction *actionMinSize=new QAction(tr(" "),this);
    connect(actionMinSize,SIGNAL(triggered()),this,SLOT(showMinimized()));
    QAction *actionMaxSize=new QAction(tr(" "),this);
    connect(actionMaxSize,SIGNAL(triggered()),this,SLOT(showMaximized()));
    QAction *actionRestore=new QAction(tr(" "),this);
    connect(actionRestore,SIGNAL(triggered()),this,SLOT(showNormal()));
    QAction *actionQuit=new QAction(tr(" "),this);
    /*
      gui qApp QApplication::instance()
       gui qApp QCoreapplication::instance()

       qApp QApplication/QCoreapplication
    */
    connect(actionQuit,SIGNAL(triggered()),qApp,SLOT(quit()));

    menu->addAction(actionMinSize);
    menu->addAction(actionMaxSize);
    menu->addAction(actionRestore);
    menu->addSeparator();
    menu->addAction(actionQuit);

    return menu;
}

void Widget::iconActived(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
    case QSystemTrayIcon::DoubleClick:
        QMessageBox::information(this,tr(" "),tr(" "));
        break;
    case QSystemTrayIcon::Trigger:
        QMessageBox::information(this,tr(" "),tr(" "));
        break;
    case QSystemTrayIcon::MiddleClick:
        QMessageBox::information(this,tr(" "),tr(" "));
        break;
    }
}

main.cpp
#include <QtGui/QApplication>
#include "widget.h"
#include <QTextCodec>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());

    // 
    QApplication::setQuitOnLastWindowClosed(false);

    Widget w;
    w.show();
    
    return a.exec();
}