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でファイルアイコンをダブルクリックした場合などです。
このイベントは、アプリケーションにリクエストを通知するためだけに使用されます。安全に無視することができます。
注意: このクラスは現在macOSでのみサポートされています。
macOS の例
macOS上でイベントをトリガーするためには、アプリケーションが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>CFBundleTypeExtensions</key> <array> <string>png</string> </array> <key>CFBundleTypeRole</key> <string>Viewer</string> </dict> </array> </dict> </plist>
次のQApplication サブクラスの実装は、QFileOpenEventを処理して、例えばアプリケーションのDockアイコンにドロップされたファイルを開く方法を示しています。
#include <QApplication> #include <QDebug> #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 を使用して開くことができるローカル・ファイルの名前であることが保証されていないことに注意してください。文字列の内容は、ソース・アプリケーションに依存します。
© 2025 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.