无法使QSystemTrayIcon与激活原因一起正常工作

时间:2012-07-19 13:49:46

标签: qt tray

我正在使用Ubuntu 12.04,虽然我可以创建一个带有可用菜单的托盘图标,但我无法控制其操作:

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/icons/Pictures/icon.png"));
    trayIcon->setToolTip(QString("Hello there..."));

    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(clickSysTrayIcon(QSystemTrayIcon::ActivationReason)));
    connect(this,SIGNAL(minimized()),this,SLOT(hide()),Qt::QueuedConnection);

    QMenu *changer_menu = new QMenu;
    Show_action = new QAction(tr("S&how"),this);
    Show_action->setIconVisibleInMenu(true);
    connect(Show_action, SIGNAL(triggered()), this, SLOT(showClicked()));
    changer_menu->addAction(Show_action);
    changer_menu->addSeparator();
    Quit_action = new QAction(tr("&Quit"), this);
    Quit_action->setIconVisibleInMenu(true);;
    connect(Quit_action, SIGNAL(triggered()), this, SLOT(close_minimize()));
    changer_menu->addAction(Quit_action);

    trayIcon->setContextMenu(changer_menu);
    trayIcon->show();

clickSysTrayIcon(QSystemTrayIcon :: ActivationReason)如下:

void MainWindow::clickSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
    //reason is a variable that holds the type of activation or click done on the icon tray
    qDebug() << "I'm in!";
}

,并在头文件中定义为:

private Q_SLOTS:
    void clickSysTrayIcon(QSystemTrayIcon::ActivationReason reason);

然而,我无法得到“我在!”要显示的消息。我尝试使用左/右点击,中键和鼠标滚轮,但我从未看到此消息被输出。

有什么问题?

编辑:似乎特定系统Ubuntu 12.04出了问题,因为它不再使用托盘图标而只使用指标。因此,有一个程序使用托盘图标,并将它们转换为指标。但是,指标的特征已经消失。我知道这是系统的责任,因为相同的程序在相同的代码下,在Lubuntu 12.04下使用LXDE桌面完美运行。

我责怪Ubuntu。 sni-qt软件包不能很好地从托盘图标迁移到指示器,只要指示器可以在点击,滚动等方面进行交互。这是一种耻辱! 解决这个问题的方法是什么?

我的恩惠结束了,所以如果有人可以解决这个问题我会感激不尽!

1 个答案:

答案 0 :(得分:2)

将问题提交给对项目影响最大的人。

https://help.ubuntu.com/community/ReportingBugs#How_to_report_bugs

https://bugreports.qt.io/

变通

我会在指示器区域顶部绘制一个浮动的无框qwidget,然后在其上添加相应的mouseEvent函数。

这是这种解决方式的起点。我不知道这是多么犹豫,但它在Windows中运行良好。我知道有一些用于Windows的UI调整和工具使用这种分层元素,如DisplayFusion和TeamViewer。我还没有在Ubuntu中测试它,但它应该以相同的方式工作。

#include <QtGui/QWidget>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QMouseEvent>
#include <QPixmap>
#include <QAction>
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <QApplication>
#include <QTimerEvent>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0)
        : QWidget(parent)
    {
        // setup this widget to be borderless, transparent around the image
        // and always on top
        // and not to have a presence in the "visible window list"
        this->setWindowFlags( Qt::WindowStaysOnTopHint |
                              Qt::FramelessWindowHint | Qt::Tool);
        this->setAttribute(Qt::WA_TranslucentBackground);

        // necessary if you want to track when you enter and leave the widget's rect with the mouse
        this->setMouseTracking(true);

        m_trayIcon = new QSystemTrayIcon(this);
        m_trayIcon->setIcon(QIcon("icon1.ico"));
        m_trayIcon->setToolTip(QString("Hello there..."));

        m_changer_menu = new QMenu;

        m_show_action = new QAction(tr("S&how"),this);
        m_show_action->setIconVisibleInMenu(true);

        connect(m_show_action, SIGNAL(triggered()), this, SLOT(showClicked()));

        m_changer_menu->addAction(m_show_action);
        m_changer_menu->addSeparator();

        m_quit_action = new QAction(tr("&Quit"), this);
        m_quit_action->setIconVisibleInMenu(true);;

        connect(m_quit_action, SIGNAL(triggered()), this, SLOT(close_minimize()));

        m_changer_menu->addAction(m_quit_action);

        m_trayIcon->setContextMenu(m_changer_menu);
        m_trayIcon->show();

        QPixmap p("icon2.ico");
        m_pix = p.scaled(QSize(m_trayIcon->geometry().width(),
                                   m_trayIcon->geometry().height()),
                             Qt::IgnoreAspectRatio,
                             Qt::SmoothTransformation);
        this->move(m_trayIcon->geometry().x() ,m_trayIcon->geometry().y());
        this->resize(m_trayIcon->geometry().width(), m_trayIcon->geometry().height());

//        qDebug() << m_trayIcon->geometry();
//        qDebug() << this->geometry();
        // This assumes that the notification is stationary.  If you want it to move
        // with the tray icon underneath, you will need to subclass QSystemTrayIcon
        // and track its move and resize and probably also its show and hide events

        // raise itself 15x a second
        this->startTimer(1000/15);
    }

    ~Widget(){ }

public slots:
    void mouseDoubleClickEvent(QMouseEvent *)
    {
        qDebug() << Q_FUNC_INFO;
    }

    void mouseReleaseEvent(QMouseEvent * me)
    {
        qDebug() << Q_FUNC_INFO;
        switch(me->button())
        {
        case Qt::LeftButton:
            qDebug() << "Left Click";
            break;
        case Qt::RightButton:
            qDebug() << "Right Click";
            m_changer_menu->popup(this->geometry().topLeft() + me->pos());
            break;
        default:
            qDebug() << "other click";
            break;
        }
    }

    void showClicked()
    {
        qDebug() << Q_FUNC_INFO;
    }

    void close_minimize()
    {
        qDebug() << Q_FUNC_INFO;
        qApp->exit();
    }

    void paintEvent(QPaintEvent *)
    {
        QPainter aPainter(this);
        aPainter.drawPixmap(rect(), m_pix);
    }

    void timerEvent(QTimerEvent *)
    {
        if(!m_changer_menu->isVisible())
            this->raise();
    }

private:
    QPixmap m_pix;
    QSystemTrayIcon * m_trayIcon;
    QMenu * m_changer_menu;
    QAction * m_quit_action;
    QAction * m_show_action;
};

这是主要功能......

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}