QFileOpenEvent Class

The QFileOpenEvent class provides an event that will be sent when there is a request to open a file or a URL. More...

Header: #include <QFileOpenEvent>
Inherits: QEvent

Public Functions

QFileOpenEvent(const QString &file)
QFileOpenEvent(const QUrl &url)
QString file() const
bool openFile(QFile &file, QIODevice::OpenMode flags) const
QUrl url() const

Detailed Description

\ingroupevents \inmodule QtGui

File open events will be sent to the QApplication::instance() when the operating system requests that a file or URL should be opened. This is a high-level event that can be caused by different user actions depending on the user's desktop environment; for example, double clicking on an file icon in the Finder on macOS.

This event is only used to notify the application of a request. It may be safely ignored.

Note: This class is currently supported for macOS only.

macOS Example

In order to trigger the event on macOS, the application must be configured to let the OS know what kind of file(s) it should react on.

For example, the following Info.plist file declares that the application can act as a viewer for files with a PNG extension:

 <?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>

The following implementation of a QApplication subclass shows how to handle QFileOpenEvent to open the file that was, for example, dropped on the Dock icon of the application.

 #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);
     }
 };

Note how QFileOpenEvent::file() is not guaranteed to be the name of a local file that can be opened using QFile. The contents of the string depend on the source application.

Member Function Documentation

[explicit] QFileOpenEvent::QFileOpenEvent(const QString &file)

\internal

Constructs a file open event for the given file.

[explicit] QFileOpenEvent::QFileOpenEvent(const QUrl &url)

\internal

Constructs a file open event for the given url.

QString QFileOpenEvent::file() const

Returns the name of the file that the application should open.

This is not guaranteed to be the path to a local file.

bool QFileOpenEvent::openFile(QFile &file, QIODevice::OpenMode flags) const

\deprecated[6.6] interpret the string returned by file()

Opens a QFile on the file referenced by this event in the mode specified by flags. Returns true if successful; otherwise returns false.

This is necessary as some files cannot be opened by name, but require specific information stored in this event.

\since4.8

QUrl QFileOpenEvent::url() const

Returns the url that the application should open.

\since4.6