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 属于事件类。
公共函数
受保护函数
| QFileOpenEvent(QFileOpenEvent &&) | |
| QFileOpenEvent & | operator=(QFileOpenEvent &&) |
| QFileOpenEvent & | operator=(const QFileOpenEvent &other) |
详细说明
当操作系统请求打开文件或 URL 时,文件打开事件将发送至 `QApplication::instance()`。 这是一个高级事件,根据平台的不同,可能由不同的用户操作触发;例如,在 Finder 中双击文件,在 macOS 上将文件拖放到应用程序的 Dock 图标上,或者在 iOS 上从另一个应用程序共享文件。
此事件仅用于通知应用程序存在请求。如果文件不应被打开,可以安全地忽略该事件。
Apple 平台
要在 Apple 平台上触发该事件,必须对应用程序进行配置,以便让操作系统知道它应响应哪些类型的文件。
例如,以下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 子类的实现示例展示了如何处理QFileOpenEvent,以打开例如被拖放到应用程序Dock图标上的文件。
#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 实例。该函数已被删除。
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.