QAbstractFileEngineHandler

Detailed Description

The PySide.QtCore.QAbstractFileEngineHandler class provides a way to register custom file engines with your application.

PySide.QtCore.QAbstractFileEngineHandler is a factory for creating PySide.QtCore.QAbstractFileEngine objects (file engines), which are used internally by PySide.QtCore.QFile , PySide.QtCore.QFileInfo , and PySide.QtCore.QDir when working with files and directories.

When you open a file, Qt chooses a suitable file engine by passing the file name from PySide.QtCore.QFile or PySide.QtCore.QDir through an internal list of registered file engine handlers. The first handler to recognize the file name is used to create the engine. Qt provides internal file engines for working with regular files and resources, but you can also register your own PySide.QtCore.QAbstractFileEngine subclasses.

To install an application-specific file engine, you subclass PySide.QtCore.QAbstractFileEngineHandler and reimplement PySide.QtCore.QAbstractFileEngineHandler.create() . When you instantiate the handler (e.g. by creating an instance on the stack or on the heap), it will automatically register with Qt. (The latest registered handler takes precedence over existing handlers.)

For example:

class ZipEngineHandler(QAbstractFileEngineHandler):
    def create(self, fileName):
        # ZipEngineHandler returns a ZipEngine for all .zip files
        if fileName.toLower().endsWith(".zip"):
            return ZipEngine(fileName)
        else
            return None

def main():
    app = QApplication(sys.argv)

    engine = ZipEngineHandler()

    window = MainWindow()
    window.show()

    return app.exec()

When the handler is destroyed, it is automatically removed from Qt.

The most common approach to registering a handler is to create an instance as part of the start-up phase of your application. It is also possible to limit the scope of the file engine handler to a particular area of interest (e.g. a special file dialog that needs a custom file engine). By creating the handler inside a local scope, you can precisely control the area in which your engine will be applied without disturbing file operations in other parts of your application.

class PySide.QtCore.QAbstractFileEngineHandler

Constructs a file handler and registers it with Qt. Once created this handler’s PySide.QtCore.QAbstractFileEngineHandler.create() function will be called (along with all the other handlers) for any paths used. The most recently created handler that recognizes the given path (i.e. that returns a PySide.QtCore.QAbstractFileEngine ) is used for the new path.

PySide.QtCore.QAbstractFileEngineHandler.create(fileName)
Parameters:fileName – unicode
Return type:PySide.QtCore.QAbstractFileEngine

Creates a file engine for file fileName . Returns 0 if this file handler cannot handle fileName .

Example:

def create(fileName):
    # ZipEngineHandler returns a ZipEngine for all .zip files
    if fileName.lower().endswith(".zip"):
        return ZipEngine(fileName)
    else
        return None