• 10440阅读
  • 4回复

QT如果让应用程序最小化到托盘(Tray)? [复制链接]

上一主题 下一主题
离线leaway
 

只看楼主 正序阅读 楼主  发表于: 2007-01-07
QT如果让应用程序最小化到托盘(Tray)?
类似于MSN那种,在系统右下的托盘显示一个图标。。。
[ 此贴被XChinux在2007-01-07 18:46重新编辑 ]
离线dg821
只看该作者 4楼 发表于: 2008-05-31
  /**
    * 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);
    }
离线leaway

只看该作者 3楼 发表于: 2007-01-07
原来QT3没有直接的类似功能可用,不过找到一个实现例子:
http://www.linux-ren.org/modules/newbb/viewtopic.php?topic_id=2190&forum=15
离线leaway

只看该作者 2楼 发表于: 2007-01-07
我是用QT3,文档里好像没有这个类
离线XChinux

只看该作者 1楼 发表于: 2007-01-07
看看QSystemTrayIcon类
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个