Qt on Mac OS - Detecting Dock Menu Click

I drop the system tray icon for the version of my Mac OS application. However, there is one small problem: when the user closes the main window, the application should continue to work in the background, and the main window should again become visible if the user clicks on the dock icon. So far, I have not found a way to intercept this click on the icon.

Is there any way to do this with Qt? If not, how should I use my own API to implement this behavior?

I tried to create my own application class that implements QApplication so that I can reimplement macEventFilter, but the documentation for this function is scarce.

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:

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

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

closeEvent() , , X - .

0

Source: https://habr.com/ru/post/1782052/


All Articles