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#
Functions#
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.
- class PySide6.QtGui.QFileOpenEvent(arg__1)#
PySide6.QtGui.QFileOpenEvent(file)
PySide6.QtGui.QFileOpenEvent(url)
- Parameters:
arg__1 –
PySide6.QtGui.QFileOpenEvent
file – str
url –
PySide6.QtCore.QUrl
- PySide6.QtGui.QFileOpenEvent.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.
- PySide6.QtGui.QFileOpenEvent.openFile(file, flags)#
- Parameters:
file –
PySide6.QtCore.QFile
flags – Combination of
QIODeviceBase.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 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.
- PySide6.QtGui.QFileOpenEvent.url()#
- Return type:
Returns the url that the application should open.