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
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
File open events will be sent to the
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.
This class is currently supported for macOS only.
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)
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.
- class PySide6.QtGui.QFileOpenEvent(arg__1)#
- Return type:
Returns the name of the file that the application should open.
This is not guaranteed to be the path to a local file.
- PySide6.QtGui.QFileOpenEvent.openFile(file, flags)#
- Return type:
This function is deprecated.
interpret the string returned by
QFile on the
file referenced by this event in the mode specified by
true if successful; otherwise returns
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.