简体   繁体   English

Qt在Mac OS上 - 检测停靠菜单上的点击

[英]Qt on Mac OS - Detecting a click on the dock menu

I am dropping the system tray icon for the Mac OS version of my application. 我正在删除我的应用程序的Mac OS版本的系统托盘图标。 However there's one small problem: when the user closes the main window the application is supposed to continue to run on the background and the main window should become visible again if the user clicks the dock icon. 但是有一个小问题:当用户关闭主窗口时,应用程序应该继续在后台运行,如果用户单击停靠图标,主窗口将再次可见。 So far I have found no way of intercepting this click on the icon. 到目前为止,我发现没有办法拦截图标上的这个点击。

Is there any way to accomplish this with Qt? 有没有办法用Qt实现这个目标? If not, how should I proceed with the native API to implement this behavior? 如果没有,我应该如何继续使用本机API来实现此行为?

I have tried to create a custom application class that implement QApplication so that I can re-implement the macEventFilter but the documentation on this function is scarce. 我试图创建一个实现QApplication的自定义应用程序类,以便我可以重新实现macEventFilter,但是这个函数的文档很少。

application.h: application.h:

#ifndef APPLICATION_H
#define APPLICATION_H

#include <QApplication>

class QWidget;

class Application : public QApplication
{
    Q_OBJECT

public:

    Application(int, char*[]);
    void setMainWidget(QWidget*);
    bool macEventFilter(EventHandlerCallRef, EventRef);

private:
    QWidget *mainWidget;
};

#endif // APPLICATION_H

application.cpp: application.cpp:

#include <Application.h>
#include <QWidget>

Application::Application(int argc, char *argv[])
    : QApplication(argc, argv)
{
}

void Application::setMainWidget(QWidget *mainWidget)
{
    this->mainWidget = mainWidget;
}

bool Application::macEventFilter(EventHandlerCallRef, EventRef)
{
    mainWidget->show();
    return false;
}

main.cpp: main.cpp中:

    #include <QtCore>
    #include <Application.h>
    #include "mainwidget.h"

    int main(int argc, char *argv[]) {
        Application a(argc, argv);

        MainWidget mainWidget;

    #ifdef Q_WS_MAC

        a.setWindowIcon(QIcon(":/resource/army-officer-icon.png"));

    #endif

        a.setMainWidget((QWidget*)&mainWidget);

        mainWidget.show();

        return a.exec(); 
    }

您需要重新实现窗口的closeEvent() ,然后检查事件是来自X按钮还是其他地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM