Chinaunix首页 | 论坛 | 博客
  • 博客访问: 619580
  • 博文数量: 178
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-12 20:06
文章分类

全部博文(178)

文章存档

2011年(1)

2010年(94)

2009年(86)

我的朋友

分类:

2009-12-25 15:18:17

可以在changeEvent(QEvent * event )函数中实现:
void **::changeEvent(QEvent *e)
{
    //QMainWindow::changeEvent(e);

    if (e->WindowStateChange)
    {
        switch (this->windowState())
        {
        case Qt::WindowMinimized:
            {
                trayicon->show();
                this->hide();
                //e->ignore();
            }
        default :break;
        }
    }

    if (this->isVisible())
    {
        trayicon->hide();
    }

    switch (e->type())
    {
        case QEvent::LanguageChange:
        {
            ui->retranslateUi(this);
            break;
        }

        default:
        break;
    }
}

下面这段代码是摘自:http://blog.163.com/ak_19860216/blog/static/128071932009231115142757/,
有一定的借鉴。

#include

class myclass: public QWidget

{

public:

        myclass();

private:

        QPushButton *b1;

        QSystemTrayIcon *trayIcon;       

};

myclass::myclass()

{       

        QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));

        setMinimumSize(200,100);

        setMaximumSize(200,100);

        b1=new QPushButton("hello world",this);

        b1->setGeometry(20,20,160,40);

        QIcon icon = QIcon("./images/bad.svg");

        setWindowIcon(icon);

        trayIcon = new QSystemTrayIcon(this);

        trayIcon->setIcon(icon);

        trayIcon->setToolTip("a trayicon example");

        QString titlec=tr("Hello");

        QString textc=QString::fromLocal8Bit("Hello QT?");

        trayIcon->show();

        trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);

        setWindowTitle(tr("Systray"));

}

int main(int argc,char **argv)

{

        QApplication testc(argc,argv);

        myclass newc;

        newc.show();

        return testc.exec();

}

第二期:

进一步研究,加入点击托盘的响应,使提示信息显示在托盘上,托盘的右键菜单。

经验一:编译信号和槽 需要有.h .cpp这样的格式,否则不会生成相应的信号和槽;

经验二:信号和槽之间可以递参数,但是不允许函数直接给槽提供参数。
hello.h

#include
class myclass: public QWidget
{
Q_OBJECT
public:
myclass();
private:
QPushButton *b1;
QSystemTrayIcon *trayIcon;
char *msg;
void showMessage(char *msg);
private slots:
void showM();
void iconActivated(QSystemTrayIcon::ActivationReason reason);
};

hello.cpp

#include
#include "hello.h"
myclass::myclass()
{
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
setMinimumSize(200,100);
setMaximumSize(200,100);
b1=new QPushButton("hello world",this);
b1->setGeometry(20,20,160,40);
QIcon icon = QIcon("./images/bad.svg");
setWindowIcon(icon);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(icon);
trayIcon->setToolTip("a trayicon example");
trayIcon->show();
setWindowTitle(tr("Systray"));

connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                     this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
connect(b1,SIGNAL(clicked()),this,SLOT(showM()));
}

void myclass::showM()
{
QString titlec=tr("slot需要signal相同的参数进行传递");
QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
}

void myclass::showMessage(char *msg)
{
QString titlec=tr(msg);
QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
}

void myclass::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
showMessage("单击");
break;
case QSystemTrayIcon::DoubleClick:
showMessage("双击啦");
break;
case QSystemTrayIcon::MiddleClick:
showMessage("你用的是三轮鼠标还是滚轮鼠标啊");
break;
default:
;
}
}

int main(int argc,char **argv)
{
QApplication testc(argc,argv);
myclass newc;
newc.show();
return testc.exec();
}

第三期:

那么最后我们把所有元素都结合在一起来实现这么一个托盘程序,比较上个例子我们增加了右键菜单并使它管理窗口的大小。
hello.h

#include
class myclass: public QWidget
{
        Q_OBJECT       
public:
        myclass();
private:
        QPushButton *b1;
        QSystemTrayIcon *trayIcon;
        char *msg;
        void showMessage(char *msg);
        void createActions();
        void createTrayIcon();
        QAction *minimizeAction;
        QAction *maximizeAction;
        QAction *restoreAction;
        QAction *quitAction;
        QMenu *trayIconMenu;
private slots:
        void showM();
        void iconActivated(QSystemTrayIcon::ActivationReason reason);
};

hello.cpp

#include
#include "hello.h"
myclass::myclass()
{       
        QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
        setMinimumSize(200,100);
        b1=new QPushButton("hello world",this);
        b1->setGeometry(20,20,160,40);
        QIcon icon = QIcon("./images/bad.svg");
        setWindowIcon(icon);
        trayIcon = new QSystemTrayIcon(this);
        trayIcon->setIcon(icon);
        trayIcon->setToolTip("a trayicon example");
createActions();
createTrayIcon();
        trayIcon->show();
        setWindowTitle(tr("Systray"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
connect(b1,SIGNAL(clicked()),this,SLOT(showM()));
}

void myclass::showM()
{
        QString titlec=tr("slot需要signal相同的参数进行传递");
        QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
        trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
      }
               
void myclass::showMessage(char *msg)
{
        QString titlec=tr(msg);
        QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
        trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000);
      }
               
void myclass::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
     switch (reason) {
     case QSystemTrayIcon::Trigger:
showMessage("单击");
        break;
     case QSystemTrayIcon::DoubleClick:
showMessage("双击啦");
         break;
     case QSystemTrayIcon::MiddleClick:
showMessage("你用的是三轮鼠标还是滚轮鼠标啊");
         break;
     default:
         ;
     }
}

void myclass::createActions()
{
     minimizeAction = new QAction(tr("最小化 (&I)"), this);
     connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

     maximizeAction = new QAction(tr("最大化 (&X)"), this);
     connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

     restoreAction = new QAction(tr("还原 (&R)"), this);
     connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

     quitAction = new QAction(tr("退出 (&Q)"), this);
     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void myclass::createTrayIcon()
{
     trayIconMenu = new QMenu(this);
     trayIconMenu->addAction(minimizeAction);
     trayIconMenu->addAction(maximizeAction);
     trayIconMenu->addAction(restoreAction);
     trayIconMenu->addSeparator();
     trayIconMenu->addAction(quitAction);
     trayIcon->setContextMenu(trayIconMenu);
}


以下摘自Qt中文论坛:
/**
    * build the SystemTroyIcon
    *
    */
    public void SystemTroyIconDisplay()
    {
        //if could't view systemTrayIcon, not to set the icon
        if (!QSystemTrayIcon.isSystemTrayAvailable())
        {
            return;
        }
        trayIconMenu = new QMenu(this);
        trayIconMenu.aboutToShow.connect(this, "updateMenu()");

        toggleVisibilityAction = new QAction("Show/Hide", this);
        toggleVisibilityAction.triggered.connect(this, "toggleVisibility()");
        trayIconMenu.addAction(toggleVisibilityAction);

        QAction restoreAction = new QAction("Restore", this);
        restoreAction.triggered.connect(this, "showNormal()");
        trayIconMenu.addAction(restoreAction);

        QAction minimizeAction = new QAction("Minimize", this);
        minimizeAction.triggered.connect(this, "showMinimized()");
        trayIconMenu.addAction(minimizeAction);

        QAction maximizeAction = new QAction("Maximize", this);
        maximizeAction.triggered.connect(this, "showMaximized()");
        trayIconMenu.addAction(maximizeAction);

        trayIconMenu.addSeparator();

        QAction quitAction = new QAction("&Quit", this);
        quitAction.triggered.connect(this, "QuitApplication()");
        trayIconMenu.addAction(quitAction);

        // Create the tray icon
        trayIcon = new QSystemTrayIcon(this);
        trayIcon.setToolTip("M2 Application");
        trayIcon.setContextMenu(trayIconMenu);

        trayIcon.activated.connect(this, "activated(com.trolltech.qt.gui.QSystemTrayIcon$ActivationReason)");

        changeIcon(0);
        trayIcon.setIcon(new QIcon(new QPixmap("classpath:/com/macchy/m2/ui/images/Skype Time.png")));
        trayIcon.show();
    }

    /**
    * quit the application
    */
    protected void QuitApplication()
    {
        //stop the application
        QApplication.exit();
        trayIcon.dispose();
    }

    /**
    * change the view of hide or show
    */
    protected void updateMenu() {
        toggleVisibilityAction.setText(isVisible() ? tr("Hide") : tr("Show"));
    }

    protected void toggleVisibility() {
        if (isVisible())
            hide();
        else
            show();
    }

    /**
    * @param reason
    */
    public void activated(QSystemTrayIcon.ActivationReason reason) {
        //String name = QSystemTrayIcon.MessageIcon.resolve(reason.value()).name();
        if (reason.equals(QSystemTrayIcon.ActivationReason.Trigger)){
            if(!isVisible())
            {
                show();
            }
        }
    }

然后在重写closeEvent
    @Override
    protected void closeEvent(QCloseEvent e)
    {
              e.ignore();
              setVisible(false);
    }

阅读(1316) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~