QFileOpenEvent Class
QFileOpenEventクラスは、ファイルやURLを開く要求があったときに送られるイベントを提供します。詳細...
| ヘッダー | #include <QFileOpenEvent> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui)target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 継承: | QEvent |
- 継承されたメンバを含むすべてのメンバのリスト
- 非推奨メンバー
- QFileOpenEventはイベントクラスの一部です。
パブリック関数
詳細説明
ファイル・オープン・イベントは、オペレーティング・システムからファイルまたは 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では、Filesアプリケーションなどでアプリケーションが "Open With "アクションとして表示されるために、以下のキーが必要です:
<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 を使って開くことのできるローカル・ファイルの名前であることが保証されていないことに注意してください。文字列の内容は、ソース・アプリケーションに依存します。
© 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.