Qt 4.8
Public Functions | Static Public Functions | Properties | Related Functions | List of all members
QPluginLoader Class Reference

The QPluginLoader class loads a plugin at run-time. More...

#include <qpluginloader.h>

Inheritance diagram for QPluginLoader:
QObject

Public Functions

QString errorString () const
 Returns a text string with the description of the last error that occurred. More...
 
QString fileName () const
 
QObjectinstance ()
 Returns the root component object of the plugin. More...
 
bool isLoaded () const
 Returns true if the plugin is loaded; otherwise returns false. More...
 
bool load ()
 Loads the plugin and returns true if the plugin was loaded successfully; otherwise returns false. More...
 
QLibrary::LoadHints loadHints () const
 
 QPluginLoader (QObject *parent=0)
 Constructs a plugin loader with the given parent. More...
 
 QPluginLoader (const QString &fileName, QObject *parent=0)
 Constructs a plugin loader with the given parent that will load the plugin specified by fileName. More...
 
void setFileName (const QString &fileName)
 
void setLoadHints (QLibrary::LoadHints loadHints)
 
bool unload ()
 Unloads the plugin and returns true if the plugin could be unloaded; otherwise returns false. More...
 
 ~QPluginLoader ()
 Destroys the QPluginLoader object. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Static Public Functions

static QObjectList staticInstances ()
 Returns a list of static plugin instances (root components) held by the plugin loader. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 

Properties

QLibraryPrivated
 
bool did_load
 
QString fileName
 the file name of the plugin More...
 
QLibrary::LoadHints loadHints
 Give the load() function some hints on how it should behave. More...
 

Related Functions

(Note that these are not member functions.)

void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction (QtPluginInstanceFunction function)
 Registers the given function with the plugin loader. More...
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QPluginLoader class loads a plugin at run-time.

Note
This class or function is reentrant.

QPluginLoader provides access to a How to Create Qt Plugins{Qt plugin}. A Qt plugin is stored in a shared library (a DLL) and offers these benefits over shared libraries accessed using QLibrary:

An instance of a QPluginLoader object operates on a single shared library file, which we call a plugin. It provides access to the functionality in the plugin in a platform-independent way. To specify which plugin to load, either pass a file name in the constructor or set it with setFileName().

The most important functions are load() to dynamically load the plugin file, isLoaded() to check whether loading was successful, and instance() to access the root component in the plugin. The instance() function implicitly tries to load the plugin if it has not been loaded yet. Multiple instances of QPluginLoader can be used to access the same physical plugin.

Once loaded, plugins remain in memory until all instances of QPluginLoader has been unloaded, or until the application terminates. You can attempt to unload a plugin using unload(), but if other instances of QPluginLoader are using the same library, the call will fail, and unloading will only happen when every instance has called unload(). Right before the unloading happen, the root component will also be deleted.

In order to speed up loading and validation of plugins, some of the information that is collected during loading is cached in persistent memory (through QSettings). For instance, the result of a load operation (e.g. succeeded or failed) is stored in the cache, so that subsequent load operations don't try to load an invalid plugin. However, if the "last modified" timestamp of a plugin has changed, the plugin's cache entry is invalidated and the plugin is reloaded regardless of the values in the cache entry. The cache entry is then updated with the new result of the load operation.

This also means that the timestamp must be updated each time the plugin or any dependent resources (such as a shared library) is updated, since the dependent resources might influence the result of loading a plugin.

See How to Create Qt Plugins for more information about how to make your application extensible through plugins.

Note that the QPluginLoader cannot be used if your application is statically linked against Qt. In this case, you will also have to link to plugins statically. You can use QLibrary if you need to load dynamic libraries in a statically linked application.

Note
In Symbian the plugin stub files must be used whenever a path to plugin is needed. For the purposes of loading plugins, the stubs can be considered to have the same name as the actual plugin binary. In practice they have ".qtplugin" extension instead of ".dll", but this difference is handled transparently by QPluginLoader and QLibrary to avoid need for Symbian specific plugin handling in most Qt applications. Plugin stubs are needed because Symbian Platform Security denies all access to the directory where the actual plugin binaries are located.
See also
QLibrary, {Plug & Paint Example}

Definition at line 62 of file qpluginloader.h.

Constructors and Destructors

◆ QPluginLoader() [1/2]

QPluginLoader::QPluginLoader ( QObject parent = 0)
explicit

Constructs a plugin loader with the given parent.

Definition at line 144 of file qpluginloader.cpp.

145  : QObject(parent), d(0), did_load(false)
146 {
147 }
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
QLibraryPrivate * d
Definition: qpluginloader.h:89

◆ QPluginLoader() [2/2]

QPluginLoader::QPluginLoader ( const QString fileName,
QObject parent = 0 
)
explicit

Constructs a plugin loader with the given parent that will load the plugin specified by fileName.

To be loadable, the file's suffix must be a valid suffix for a loadable library in accordance with the platform, e.g. .so on Unix, - .dylib on Mac OS X, and .dll on Windows. The suffix can be verified with QLibrary::isLibrary().

Note: In Symbian the fileName must point to plugin stub file.

See also
setFileName()

Definition at line 162 of file qpluginloader.cpp.

163  : QObject(parent), d(0), did_load(false)
164 {
165  setFileName(fileName);
166 }
void setFileName(const QString &fileName)
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
QLibraryPrivate * d
Definition: qpluginloader.h:89

◆ ~QPluginLoader()

QPluginLoader::~QPluginLoader ( )

Destroys the QPluginLoader object.

Unless unload() was called explicitly, the plugin stays in memory until the application terminates.

See also
isLoaded(), unload()

Definition at line 176 of file qpluginloader.cpp.

177 {
178  if (d)
179  d->release();
180 }
QLibraryPrivate * d
Definition: qpluginloader.h:89

Functions

◆ errorString()

QString QPluginLoader::errorString ( ) const

Returns a text string with the description of the last error that occurred.

Since
4.2

Definition at line 374 of file qpluginloader.cpp.

Referenced by QDeclarativeImportDatabase::importPlugin().

375 {
376  return (!d || d->errorString.isEmpty()) ? tr("Unknown error") : d->errorString;
377 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString errorString
Definition: qlibrary_p.h:99
QLibraryPrivate * d
Definition: qpluginloader.h:89

◆ fileName()

QString QPluginLoader::fileName ( ) const

Referenced by setFileName().

◆ instance()

QObject * QPluginLoader::instance ( )

Returns the root component object of the plugin.

The plugin is loaded if necessary. The function returns 0 if the plugin could not be loaded or if the root component object could not be instantiated.

If the root component object was destroyed, calling this function creates a new instance.

The root component, returned by this function, is not deleted when the QPluginLoader is destroyed. If you want to ensure that the root component is deleted, you should call unload() as soon you don't need to access the core component anymore. When the library is finally unloaded, the root component will automatically be deleted.

The component object is a QObject. Use qobject_cast() to access interfaces you are interested in.

See also
load()

Definition at line 202 of file qpluginloader.cpp.

Referenced by QScriptEngine::availableExtensions(), QScriptEngine::importExtension(), QDeclarativeImportDatabase::importPlugin(), QDeclarativeDebugServerPrivate::loadConnectionPlugin(), and QDeclarativeInspectorService::loadInspectorPlugin().

203 {
204  if (!load())
205  return 0;
206  if (!d->inst && d->instance)
207  d->inst = d->instance();
208  return d->inst.data();
209 }
bool load()
Loads the plugin and returns true if the plugin was loaded successfully; otherwise returns false...
QWeakPointer< QObject > inst
Definition: qlibrary_p.h:94
QtPluginInstanceFunction instance
Definition: qlibrary_p.h:95
T * data() const
QLibraryPrivate * d
Definition: qpluginloader.h:89

◆ isLoaded()

bool QPluginLoader::isLoaded ( ) const

Returns true if the plugin is loaded; otherwise returns false.

See also
load()

Definition at line 266 of file qpluginloader.cpp.

267 {
268  return d && d->pHnd && d->instance;
269 }
QtPluginInstanceFunction instance
Definition: qlibrary_p.h:95
HINSTANCE pHnd
Definition: qlibrary_p.h:81
QLibraryPrivate * d
Definition: qpluginloader.h:89

◆ load()

bool QPluginLoader::load ( )

Loads the plugin and returns true if the plugin was loaded successfully; otherwise returns false.

Since instance() always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the plugin loaded in advance, in which case you would use this function.

See also
unload()

Definition at line 221 of file qpluginloader.cpp.

Referenced by QDeclarativeImportDatabase::importPlugin(), instance(), QDeclarativeDebugServerPrivate::loadConnectionPlugin(), and QDeclarativeInspectorService::loadInspectorPlugin().

222 {
223  if (!d || d->fileName.isEmpty())
224  return false;
225  if (did_load)
226  return d->pHnd && d->instance;
227  if (!d->isPlugin())
228  return false;
229  did_load = true;
230  return d->loadPlugin();
231 }
bool isPlugin(QSettings *settings=0)
Definition: qlibrary.cpp:669
QtPluginInstanceFunction instance
Definition: qlibrary_p.h:95
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString fileName
Definition: qlibrary_p.h:83
HINSTANCE pHnd
Definition: qlibrary_p.h:81
bool loadPlugin()
Definition: qlibrary.cpp:517
QLibraryPrivate * d
Definition: qpluginloader.h:89

◆ loadHints()

QLibrary::LoadHints QPluginLoader::loadHints ( ) const

◆ setFileName()

void QPluginLoader::setFileName ( const QString fileName)

Definition at line 292 of file qpluginloader.cpp.

Referenced by QDeclarativeDebugServerPrivate::loadConnectionPlugin(), and QPluginLoader().

293 {
294 #if defined(QT_SHARED)
295  QLibrary::LoadHints lh;
296  if (d) {
297  lh = d->loadHints;
298  d->release();
299  d = 0;
300  did_load = false;
301  }
302 
303 #if defined(Q_OS_SYMBIAN)
304  // In Symbian we actually look for plugin stub, so modify the filename
305  // to make canonicalFilePath find the file, if .dll is specified.
306  QFileInfo fi(fileName);
307 
308  if (fi.suffix() == QLatin1String("dll")) {
309  QString stubName = fileName;
310  stubName.chop(3);
311  stubName += QLatin1String("qtplugin");
312  fi = QFileInfo(stubName);
313  }
314 
315  QString fn = fi.canonicalFilePath();
316  // If not found directly, check also all the available drives
317  if (!fn.length()) {
318  QString stubPath(fi.fileName().length() ? fi.absoluteFilePath() : QString());
319  if (stubPath.length() > 1) {
320  if (stubPath.at(1).toAscii() == ':')
321  stubPath.remove(0,2);
322  QFileInfoList driveList(QDir::drives());
323  RFs rfs = qt_s60GetRFs();
324  foreach(const QFileInfo& drive, driveList) {
325  QString testFilePath(drive.absolutePath() + stubPath);
326  testFilePath = QDir::cleanPath(testFilePath);
327  // Use native Symbian code to check for file existence, because checking
328  // for file from under non-existent protected dir like E:/private/<uid> using
329  // QFile::exists causes platform security violations on most apps.
330  QString nativePath = QDir::toNativeSeparators(testFilePath);
331  TPtrC ptr(qt_QString2TPtrC(nativePath));
332  TUint attributes;
333  TInt err = rfs.Att(ptr, attributes);
334  if (err == KErrNone) {
335  fn = testFilePath;
336  break;
337  }
338  }
339  }
340  }
341 
342 #else
343  QString fn = QFileInfo(fileName).canonicalFilePath();
344 #endif
345 
347  d->loadHints = lh;
348  if (fn.isEmpty())
349  d->errorString = QLibrary::tr("The shared library was not found.");
350 #else
351  if (qt_debug_component()) {
352  qWarning("Cannot load %s into a statically linked Qt library.",
353  (const char*)QFile::encodeName(fileName));
354  }
355  Q_UNUSED(fileName);
356 #endif
357 }
static QLibraryPrivate * findOrCreate(const QString &fileName, const QString &version=QString())
Definition: qlibrary.cpp:438
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString canonicalFilePath() const
Returns the canonical path including the file name, i.e.
Definition: qfileinfo.cpp:551
Q_CORE_EXPORT void qWarning(const char *,...)
const T * ptr(const T &t)
static QFileInfoList drives()
Returns a list of the root directories on this system.
Definition: qdir.cpp:1812
static QString cleanPath(const QString &path)
Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path...
Definition: qdir.cpp:2082
QString errorString
Definition: qlibrary_p.h:99
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
QLibraryPrivate * d
Definition: qpluginloader.h:89
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
QString fileName() const
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
QLibrary::LoadHints loadHints
Definition: qlibrary_p.h:100
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QString absolutePath() const
Returns a file&#39;s path absolute path.
Definition: qfileinfo.cpp:577
bool qt_debug_component()
Definition: qlibrary.cpp:1319

◆ setLoadHints()

void QPluginLoader::setLoadHints ( QLibrary::LoadHints  loadHints)

Definition at line 399 of file qpluginloader.cpp.

400 {
401  if (!d) {
402  d = QLibraryPrivate::findOrCreate(QString()); // ugly, but we need a d-ptr
403  d->errorString.clear();
404  }
405  d->loadHints = loadHints;
406 }
QLibrary::LoadHints loadHints() const
static QLibraryPrivate * findOrCreate(const QString &fileName, const QString &version=QString())
Definition: qlibrary.cpp:438
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString errorString
Definition: qlibrary_p.h:99
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QLibraryPrivate * d
Definition: qpluginloader.h:89
QLibrary::LoadHints loadHints
Definition: qlibrary_p.h:100

◆ staticInstances()

QObjectList QPluginLoader::staticInstances ( )
static

Returns a list of static plugin instances (root components) held by the plugin loader.

Definition at line 436 of file qpluginloader.cpp.

Referenced by QScriptEngine::availableExtensions(), QScriptEngine::importExtension(), QFactoryLoader::instance(), and QFactoryLoader::keys().

437 {
438  QObjectList instances;
439  StaticInstanceFunctionList *functions = staticInstanceFunctionList();
440  if (functions) {
441  for (int i = 0; i < functions->count(); ++i)
442  instances.append((*functions)[i]());
443  }
444  return instances;
445 }
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507

◆ unload()

bool QPluginLoader::unload ( )

Unloads the plugin and returns true if the plugin could be unloaded; otherwise returns false.

This happens automatically on application termination, so you shouldn't normally need to call this function.

If other instances of QPluginLoader are using the same plugin, the call will fail, and unloading will only happen when every instance has called unload().

Don't try to delete the root component. Instead rely on that unload() will automatically delete it when needed.

See also
instance(), load()

Definition at line 250 of file qpluginloader.cpp.

Referenced by QDeclarativeDebugServerPrivate::loadConnectionPlugin(), and QDeclarativeInspectorService::loadInspectorPlugin().

251 {
252  if (did_load) {
253  did_load = false;
254  return d->unload();
255  }
256  if (d) // Ouch
257  d->errorString = tr("The plugin was not loaded.");
258  return false;
259 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QString errorString
Definition: qlibrary_p.h:99
QLibraryPrivate * d
Definition: qpluginloader.h:89

Friends and Related Functions

◆ qRegisterStaticPluginInstanceFunction()

void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction ( QtPluginInstanceFunction  function)
related

Registers the given function with the plugin loader.

Since
4.4

Definition at line 427 of file qpluginloader.cpp.

428 {
429  staticInstanceFunctionList()->append(function);
430 }

Properties

◆ d

QLibraryPrivate* QPluginLoader::d
private

Definition at line 89 of file qpluginloader.h.

Referenced by errorString(), instance(), isLoaded(), load(), setFileName(), unload(), and ~QPluginLoader().

◆ did_load

bool QPluginLoader::did_load
private

Definition at line 90 of file qpluginloader.h.

Referenced by load(), setFileName(), and unload().

◆ fileName

QString QPluginLoader::fileName
private

the file name of the plugin

To be loadable, the file's suffix must be a valid suffix for a loadable library in accordance with the platform, e.g. .so on Unix, .dylib on Mac OS X, and .dll on Windows. The suffix can be verified with QLibrary::isLibrary().

If the file name does not exist, it will not be set. This property will then contain an empty string.

By default, this property contains an empty string.

Note: In Symbian the fileName must point to plugin stub file.

See also
load()

Definition at line 65 of file qpluginloader.h.

◆ loadHints

QLibrary::LoadHints QPluginLoader::loadHints
private

Give the load() function some hints on how it should behave.

Since
4.4

You can give hints on how the symbols in the plugin are resolved. By default, none of the hints are set.

See the documentation of QLibrary::loadHints for a complete description of how this property works.

See also
QLibrary::loadHints

Definition at line 66 of file qpluginloader.h.


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