Qt 4.8
Public Functions | List of all members
QAbstractFileEngineHandler Class Referenceabstract

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

#include <qabstractfileengine.h>

Public Functions

virtual QAbstractFileEnginecreate (const QString &fileName) const =0
 Creates a file engine for file fileName. More...
 
 QAbstractFileEngineHandler ()
 Constructs a file handler and registers it with Qt. More...
 
virtual ~QAbstractFileEngineHandler ()
 Destroys the file handler. More...
 

Detailed Description

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

Note
This class or function is reentrant.
Since
4.1

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

When you open a file, Qt chooses a suitable file engine by passing the file name from QFile or 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 QAbstractFileEngine subclasses.

To install an application-specific file engine, you subclass QAbstractFileEngineHandler and reimplement 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 : public QAbstractFileEngineHandler
{
public:
};
{
// ZipEngineHandler returns a ZipEngine for all .zip files
return fileName.toLower().endsWith(".zip") ? new ZipEngine(fileName) : 0;
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
ZipEngineHandler engine;
MainWindow window;
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.

See also
QAbstractFileEngine, QAbstractFileEngine::create()

Definition at line 205 of file qabstractfileengine.h.

Constructors and Destructors

◆ QAbstractFileEngineHandler()

QAbstractFileEngineHandler::QAbstractFileEngineHandler ( )

Constructs a file handler and registers it with Qt.

Once created this handler's 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 QAbstractFileEngine) is used for the new path.

See also
create()

Definition at line 136 of file qabstractfileengine.cpp.

137 {
138  QWriteLocker locker(fileEngineHandlerMutex());
140  fileEngineHandlers()->prepend(this);
141 }
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
static bool qt_file_engine_handlers_in_use

◆ ~QAbstractFileEngineHandler()

QAbstractFileEngineHandler::~QAbstractFileEngineHandler ( )
virtual

Destroys the file handler.

This will automatically unregister the handler from Qt.

Definition at line 147 of file qabstractfileengine.cpp.

148 {
149  QWriteLocker locker(fileEngineHandlerMutex());
150  // Remove this handler from the handler list only if the list is valid.
152  QAbstractFileEngineHandlerList *handlers = fileEngineHandlers();
153  handlers->removeOne(this);
154  if (handlers->isEmpty())
156  }
157 }
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
bool removeOne(const T &t)
Removes the first occurrence of value in the list and returns true on success; otherwise returns fals...
Definition: qlist.h:796
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
static bool qt_abstractfileenginehandlerlist_shutDown
static bool qt_file_engine_handlers_in_use

Functions

◆ create()

QAbstractFileEngine * QAbstractFileEngineHandler::create ( const QString fileName) const
pure virtual

Creates a file engine for file fileName.

Returns 0 if this file handler cannot handle fileName.

Example:

{
// ZipEngineHandler returns a ZipEngine for all .zip files
return fileName.toLower().endsWith(".zip") ? new ZipEngine(fileName) : 0;
}
See also
QAbstractFileEngine::create()

Referenced by qt_custom_file_engine_handler_create().


The documentation for this class was generated from the following files: