From 64c4d2e06408d0e8dbe50fbaec32be65dde8c495 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Tue, 3 Dec 2024 23:25:00 +0800 Subject: [PATCH] feat(macOS): support file open event for association --- app/mainwindow.cpp | 17 +++++++++++++++++ app/mainwindow.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index de44bf4..698d8bc 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -160,6 +160,10 @@ MainWindow::MainWindow(QWidget *parent) installResizeCapture(m_graphicsView->viewport()); installResizeCapture(m_gv); installResizeCapture(m_gv->viewport()); + +#ifdef Q_OS_MACOS + qApp->installEventFilter(this); +#endif // Q_OS_MACOS } MainWindow::~MainWindow() @@ -653,6 +657,19 @@ QSize MainWindow::sizeHint() const return QSize(710, 530); } +#ifdef Q_OS_MACOS +bool MainWindow::eventFilter(QObject *obj, QEvent *event) +{ + Q_UNUSED(obj); + if (event->type() == QEvent::FileOpen) { + QFileOpenEvent *fileOpenEvent = static_cast(event); + showUrls({fileOpenEvent->url()}); + return true; + } + return false; +} +#endif // Q_OS_MACOS + void MainWindow::on_actionOpen_triggered() { QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); diff --git a/app/mainwindow.h b/app/mainwindow.h index 2850901..7c34645 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -77,6 +77,9 @@ protected slots: protected: QSize sizeHint() const override; +#ifdef Q_OS_MACOS + bool eventFilter(QObject *obj, QEvent *event) override; +#endif // Q_OS_MACOS private slots: void on_actionOpen_triggered();