-->

无法获得QSystemTrayIcon与激活原因正常工作(Cannot get QSystemTra

2019-09-19 19:42发布

我使用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);

但是,我不能得到“我在!” 要显示的消息。 我试图使其与左/右点击工作,与中间点击鼠标滚轮,但我从来没有看到正在outputed此消息。

怎么了?

编辑:看来,什么是错的与特定的系统,Ubuntu的12.04,因为它不使用托盘图标更多,只有指标。 所以,有它使用托盘图标的程序,他们将其转换为指标。 但是,随后的指标功能都没有了。 我知道,这是责怪系统,因为同样的程序,同样的代码下,完美的作品Lubuntu 12.04之下与LXDE桌面。

我责怪Ubuntu的这一点。 该SNI-QT包不从托盘图标,指标做了很好的迁移,提供指标可以点击互动,对辊等,这是一种耻辱! 这个问题的解决方案,任何?

我的慷慨结束,因此,如果有一个人谁可以解决这个问题,我会感激!

Answer 1:

提出这一问题到对项目影响最大的人。

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

https://bugreports.qt.io/

变通

我想就您的指示被涂指示器区的顶部浮动无框QWidget的,然后添加到它相应的MouseEvent功能。

这是一个启动点,这种风格的变通。 我不知道该怎么犹太这个,但它工作得很好Windows中。 我知道有适合Windows,采用分层的元素,如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();
}


文章来源: Cannot get QSystemTrayIcon to work correctly with activation reason
标签: qt tray