- class QFileOpenEvent#
The
QFileOpenEvent
class provides an event that will be sent when there is a request to open a file or a URL. More…Synopsis#
Methods#
def
__init__()
def
file()
def
openFile()
def
url()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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.from PySide6.QtWidgets import QApplication from PySide6.QtGui import QFileOpenEvent from PySide6.QtWidgets import QPushButton class MyApplication(QApplication): # public MyApplication(int argc, char **argv) super().__init__(argc, argv) bool event(QEvent event) override if event.type() == QEvent.FileOpen: openEvent = QFileOpenEvent(event) url = openEvent.url() if url.isLocalFile(): localFile = QFile(url.toLocalFile()) # read from local file elif 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.- __init__(arg__1)#
- Parameters:
arg__1 –
QFileOpenEvent
- __init__(file)
- Parameters:
file – str
- __init__(url)
- Parameters:
url –
QUrl
- file()#
- Return type:
str
Returns the name of the file that the application should open.
This is not guaranteed to be the path to a local file.
- openFile(file, flags)#
- Parameters:
file –
QFile
flags – Combination of
OpenModeFlag
- Return type:
bool
Note
This function is deprecated.
interpret the string returned by
file()
Opens a QFile on the
file
referenced by this event in the mode specified byflags
. Returnstrue
if successful; otherwise returnsfalse
.This is necessary as some files cannot be opened by name, but require specific information stored in this event.
Returns the url that the application should open.