このページでは

QFileOpenEvent Class

QFileOpenEvent クラスは、ファイルや URL を開くリクエストがあった際に送信されるイベントを提供します。詳細...

ヘッダー: #include <QFileOpenEvent>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
継承元: QEvent

パブリック関数

QString file() const
QUrl url() const

プロテクトされた関数

QFileOpenEvent(QFileOpenEvent &&)
QFileOpenEvent &operator=(QFileOpenEvent &&)
QFileOpenEvent &operator=(const QFileOpenEvent &other)

詳細な説明

オペレーティングシステムがファイルまたは URL の開くよう要求すると、ファイルオープンイベントが `QApplication::instance()` に送信されます。 これは、プラットフォームに応じてさまざまなユーザー操作によって発生する可能性がある高レベルのイベントです。たとえば、macOS では Finder でファイルをダブルクリックしたり、ファイルをアプリケーションの Dock アイコンにドラッグしたり、iOS では別のアプリケーションからファイルを共有したりする場合などです。

このイベントは、アプリケーションにリクエストを通知するためにのみ使用されます。ファイルを開く必要がない場合は、安全に無視しても問題ありません。

Apple プラットフォーム

Appleプラットフォームでこのイベントをトリガーするには、アプリケーションがどの種類のファイルに対して反応すべきかをOSに通知するよう設定する必要があります。

たとえば、次の `Info.plist ` ファイルは、このアプリケーションが PNG ファイルのビューアとして機能できることを宣言しています:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.png</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
        </dict>
    </array>
</dict>
</plist>

また、iOS では、アプリケーションが「ファイル」アプリなどで「このアプリで開く」アクションとして表示されるようにするには、以下のキーも必要です:

<key>UISupportsDocumentBrowser</key>
<true/>

以下のQApplication サブクラスの実装例は、例えばアプリケーションのDockアイコンにドラッグ&ドロップされたファイルを開くために、QFileOpenEventを処理する方法を示しています。

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QFileOpenEvent>
#include <QPushButton>

class MyApplication : public QApplication
{
public:
    MyApplication(int &argc, char **argv)
        : QApplication(argc, argv)
    {
    }

    bool event(QEvent *event) override
    {
        if (event->type() == QEvent::FileOpen) {
            QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
            const QUrl url = openEvent->url();
            if (url.isLocalFile()) {
                QFile localFile(url.toLocalFile());
                // read from local file
            } else if (url.isValid()) {
                // process according to the URL's schema
            } else {
                // parse openEvent->file()
            }
        }

        return QApplication::event(event);
    }
};

なお、QFileOpenEvent::file() が、QFile を使用して開くことができるローカルファイル名であるとは限りません。この文字列の内容は、ソースアプリケーションによって異なります。

メンバ関数のドキュメント

[protected delete] QFileOpenEvent::QFileOpenEvent(QFileOpenEvent &&)

QFileOpenEvent のインスタンスをMoveコンストラクタで生成します。この関数は削除されています。

QString QFileOpenEvent::file() const

アプリケーションが開くべきファイル名を返します。

これがローカルファイルへのパスであるとは限りません。

QUrl QFileOpenEvent::url() const

アプリケーションが開くべきURLを返します。

[protected delete] QFileOpenEvent &QFileOpenEvent::operator=(QFileOpenEvent &&)

other をこのQFileOpenEvent インスタンスに移動・割り当てます。この関数は削除されます。

[noexcept protected default] QFileOpenEvent &QFileOpenEvent::operator=(const QFileOpenEvent &other)

other をこのQFileOpenEvent インスタンスにコピーして割り当てます。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.