Qt系统托盘

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt系统托盘相关的知识,希望对你有一定的参考价值。

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


#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/

以上是关于Qt系统托盘的主要内容,如果未能解决你的问题,请参考以下文章

centos7.9下Qt 实现系统托盘,托盘菜单,托盘消息

Qt系统托盘

Qt设置系统托盘图标和程序图标

QT程序系统托盘

Qt 系统托盘(加hover效果)

使用QT创建系统托盘