zoukankan      html  css  js  c++  java
  • Qt系统托盘

    Qt的系统托盘的使用,可比mfc中好多了!他封装了一个专门的QSystemTrayIcon类,建立系统托盘图标。
    其实在Qt提供的示例程序已经很不错了,$QTDIRexamplesdesktopsystray
    在这里简单的实现一个系统托盘功能,对其系统托盘类的使用做以演示。


    #include <QtGui>
    class Window: public QWidget
    {
        Q_OBJECT        
    public:
        Window();

    private:
        void showMessage(char *msg);
        void createActions();
        void createTrayIcon();

        QPushButton *button;
        QSystemTrayIcon *trayIcon;
        QMenu *trayIconMenu;   
        QAction *minimizeAction;
        QAction *maximizeAction;
        QAction *restoreAction;
        QAction *quitAction;

    private slots:
        void showMessage();
        void iconActivated(QSystemTrayIcon::ActivationReason reason);
    };


    #include "window.h"
    Window::Window()
    {        
        QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
        setMinimumSize(200,100);
        button = new QPushButton("Hello World",this);
        button->setGeometry(20,20,160,40);

        //建立系统托盘图标
        QIcon icon = QIcon("./images/tray.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(button, SIGNAL(clicked()), this, SLOT(showMessage()));
    }

    void Window::showMessage()
    {
        QString titlec=tr("slot需要signal相同的参数进行传递");
        QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮"); 
        trayIcon->showMessage(titlec, textc, QSystemTrayIcon::Information, 5000);
    }
                    
    void Window::showMessage(char *msg)
    {
        QString titlec=tr(msg);
        QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮"); 
        trayIcon->showMessage(titlec, textc, QSystemTrayIcon::Information, 5000);
    }
                    
    void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
    {
        //触发后台图标执行相应事件
        switch (reason)
        {
        case QSystemTrayIcon::Trigger:
            showMessage("鼠标单击!");
            break;
        case QSystemTrayIcon::DoubleClick:
            showMessage("鼠标双击!");
            break;
        case QSystemTrayIcon::MiddleClick:
            showMessage("鼠标中键!");
            break;
        default:
            break;
        }
    }

    void Window::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 Window::createTrayIcon()
    {
         //设置右键点击时弹出的菜单
         trayIconMenu = new QMenu(this);
         trayIconMenu->addAction(minimizeAction);
         trayIconMenu->addAction(maximizeAction);
         trayIconMenu->addAction(restoreAction);
         trayIconMenu->addSeparator();
         trayIconMenu->addAction(quitAction);
         trayIcon->setContextMenu(trayIconMenu);
    }


    #include "window.h"

    int main(int argc,char **argv)
    {
        QApplication app(argc,argv);
        if (!QSystemTrayIcon::isSystemTrayAvailable())
        {
            QMessageBox::critical(0, QObject::tr("Systray"),
                QObject::tr("I couldn't detect any system tray on this system."));
            return 1;
        }

        Window window;
        window.show();

        return app.exec();
    }

    http://cool.worm.blog.163.com/blog/static/64339006200842623851379/

  • 相关阅读:
    游戏引擎架构
    前瞻设计:创新型战略推动可持续变革(全彩)
    解放创意——自由人的自由联合
    python2中的__init__.py文件的作用
    python导入模块时的执行顺序
    json使用
    JQuery基本语法(部分)
    谷歌开发者工具使用
    pythonseleniumAPI
    静态、自适应、流式、响应式四种网页布局的区别
  • 原文地址:https://www.cnblogs.com/findumars/p/6250751.html
Copyright © 2011-2022 走看看