由于激活原因,无法使QSystemTrayIcon正常工作 [英] Cannot get QSystemTrayIcon to work correctly with activation reason

查看:227
本文介绍了由于激活原因,无法使QSystemTrayIcon正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ubuntu 12.04,尽管我可以使用可用菜单创建任务栏图标,但无法控制其操作:

I am using Ubuntu 12.04 and, while I can create a tray icon with a usable menu, I cannot control its actions:

    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)如下:

The clickSysTrayIcon(QSystemTrayIcon::ActivationReason) is the following:

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!";
}

,并且在头文件中定义为:

and, defined at the header file as:

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

但是,我无法获得我进入!"要显示的消息.我试图使其与左/右键,中键和鼠标滚轮一起使用,但我从未看到此消息输出.

However, I cannot get the "I'm in!" message to be shown. I've tried to make it work with left/right clicks, with middle click and with mouse wheel, but I never see this message being outputed.

怎么了?

特定系统Ubuntu 12.04似乎有问题,因为它不再使用任务栏图标,而仅使用指示器.因此,有一个程序使用托盘图标并将其转换为指示器.但是,指标的功能消失了.我知道这应该归咎于系统,因为相同的程序和相同的代码在LXDE桌面的Lubuntu 12.04下可以完美地工作.

It seems that something's wrong with the specific system, Ubuntu 12.04, because it doesn't use tray icons any more and only indicators. So, there's a program which uses the tray icons and they convert them into indicators. But, then the features of indicators are gone. I know that it's the system to blame, because the same program, under the very same code, works perfectly under Lubuntu 12.04 with the LXDE desktop.

我为此责怪Ubuntu. sni-qt软件包不能很好地将图标从托盘图标迁移到指示器,前提是指示器可以在单击,滚轮等交互作用.这真是太遗憾了! 这个问题有解决方案吗?

I blame Ubuntu for this. The sni-qt package doesn't do a very good migration from tray icons to indicators, providing that indicators can interact on click, on roller etc. It's a shame! Any solutions to this problem?

我的赏金结束了,所以如果有人可以解决这个问题,我将很感激!

My bounty ends, so if there's someone who can address the problem I would be thankful!

推荐答案

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

Bring up the issue to the people that have the most influence on the projects.

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

https://bugreports.qt.io/

我将在绘制指示器的指示器区域的顶部制作一个浮动的无框qwidget,然后在其上添加适当的mouseEvent函数.

I would make a floating frameless qwidget on top of the indicator area where your indicator gets painted, and then add the appropriate mouseEvent functions on to it.

这是这种解决方法的起点.我不知道这是怎么做到的,但是在Windows中效果很好.我知道有一些针对Windows的UI调整和工具,它们使用了这种样式的分层元素,例如DisplayFusion和TeamViewer.我尚未在Ubuntu中对其进行测试,但是它应该以相同的方式工作.

Here is starting point for this style of work-around. I don't know how kosher this is, but it works pretty well in Windows. I know there are some UI tweaks and tools for Windows that use this style of layered elements, like DisplayFusion and TeamViewer. I haven't tested it in Ubuntu yet, but it should work the same way.

#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();
}

这篇关于由于激活原因,无法使QSystemTrayIcon正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆