Qt plug-in path (note)

Source: Internet
Author: User

Qt manual has already introduced deploying plugins
. Qt plug-in learning six months ago (1)
Also, a few path-related issues were simply sorted out.

However, it has never been clarified:How are image plug-ins, codec plug-ins, and database plug-ins loaded?

Just watching

If you want to open or save an image in JPG format, you need to load the JPG plugin. Where to find plug-ins:

Superficial answer:

$QTDIR/plugins/imageformats/you_app_path/imageformats/

Note: If you only want the plug-in to work and are not interested in others, create an imageformats directory in the directory where your application is located and put the plug-in. Create subdirectories for other plug-ins.

Real answer

 

QStringList QCoreApplication::libraryPaths()

For the image plug-in, the program traverses the string list and returns each string and/Imageformats
Create a new path after the connection, and then try to load the dynamic libraries under these paths in turn.

Aspect code (with deletions)

void QFactoryLoader::update(){    QStringList paths = QCoreApplication::libraryPaths();    for (int i = 0; i < paths.count(); ++i) {        const QString &pluginDir = paths.at(i);        QString path = pluginDir + d->suffix;        QStringList plugins = QDir(path).entryList(QDir::Files);        QLibraryPrivate *library = 0;        for (int j = 0; j < plugins.count(); ++j) {            QString fileName = QDir::cleanPath(path + QLatin1Char('/') + plugins.at(j));             library = QLibraryPrivate::findOrCreate(QFileInfo(fileName).canonicalFilePath());        }     }...

For the image plug-in,D-> suffix
Yes/Imageformats
This is achieved through a static loader () function with the return value qfactoryloader:

Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,                          (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
Not feeling right?

It seems wrong? The answer is not just what we get on the surface, and there are many things mentioned in manual or other articles, such:

  • Qlibraryinfo: location (qlibraryinfo: pluginspath
    )

  • :/QT/etc/Qt. conf
  • Qt_plugin_path
  • ...
  • Qcoreapplication: addlibrarypath ()
  • Qcoreapplication: setlibrarypaths ()

Except for the two directly related libraries, librarypaths does not seem to have any connection. How can you sayOnly
And qcoreapplication: librarypaths ??!!

Try to talk with facts...

Qcoreapplication: librarypaths ()
  • Create an empty qstringlist
  • Set
    Qlibraryinfo: location (qlibraryinfo: pluginspath)
     
    Add path

  • Make sure that the path of the application isQcoreapplication: applicationfilepath ()
    Join

  • Set
    Qt_plugin_path
    Add environment variables to the specified path in sequence

Source code:

QStringList QCoreApplication::libraryPaths(){    if (!coreappdata()->app_libpaths) {        QStringList *app_libpaths = coreappdata()->app_libpaths = new QStringList;        QString installPathPlugins =  QLibraryInfo::location(QLibraryInfo::PluginsPath);        if (QFile::exists(installPathPlugins)) {            // Make sure we convert from backslashes to slashes.            installPathPlugins = QDir(installPathPlugins).canonicalPath();            if (!app_libpaths->contains(installPathPlugins))                app_libpaths->append(installPathPlugins);        }        // If QCoreApplication is not yet instantiated,        // make sure we add the application path when we construct the QCoreApplication        if (self) self->d_func()->appendApplicationPathToLibraryPaths();        const QByteArray libPathEnv = qgetenv("QT_PLUGIN_PATH");        if (!libPathEnv.isEmpty()) {#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)            QLatin1Char pathSep(';');#else            QLatin1Char pathSep(':');#endif            QStringList paths = QString::fromLatin1(libPathEnv).split(pathSep, QString::SkipEmptyParts);            for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {                QString canonicalPath = QDir(*it).canonicalPath();                if (!canonicalPath.isEmpty()                    && !app_libpaths->contains(canonicalPath)) {                    app_libpaths->append(canonicalPath);                }            }        }...

It seems much more comfortable now,Qlibraryinfo: location (qlibraryinfo: pluginspath)
How did you get it?

Qlibraryinfo: location (qlibraryinfo: pluginspath)

It is affected by two factors:

  • If a qualified QT. conf file exists, the content of the file will be used.
  • If such a QT. conf file is not available, the dead content will be written during QT compilation.

First read the latter:

Configure

The first step to compile QT is configure, which will generate the qconfig. h and qconfig. cpp files in the src/corelib/global directory.

Open the qconfig. cpp file (you can see content similar to the following ):

static const char qt_configure_installation          [11  + 12] = "qt_instdate=2011-06-08";static const char qt_configure_prefix_path_str       [512 + 12] = "qt_prfxpath=E://Qt5//qtbase-build";static const char qt_configure_binaries_path_str     [512 + 12] = "qt_binspath=E://Qt5//qtbase-build//bin";static const char qt_configure_plugins_path_str      [512 + 12] = "qt_plugpath=E://Qt5//qtbase-build//plugins";#define QT_CONFIGURE_BINARIES_PATH qt_configure_binaries_path_str + 12;#define QT_CONFIGURE_PLUGINS_PATH qt_configure_plugins_path_str + 12;

Note: Macro qt_configure_plugins_path points
"Qt_plugpath = E: // qt5 // qtbase-build // plugins ";

Qt. conf
  • Check whether the resource file contains
    :/QT/etc/Qt. conf

  • If the application does not exist, check whether the QT. conf file is in the Application Path.
  • If yes, the corresponding qsettings will be returned; otherwise, 0 will be returned.

 

QSettings *QLibraryInfoPrivate::findConfiguration(){    QString qtconfig = QLatin1String(":/qt/etc/qt.conf");    if (!QFile::exists(qtconfig) && QCoreApplication::instance()) {            {                QDir pwd(QCoreApplication::applicationDirPath());                qtconfig = pwd.filePath(QLatin1String("qt.conf"));            }    }    if (QFile::exists(qtconfig))        return new QSettings(qtconfig, QSettings::IniFormat);    return 0; }
Merge into one

Directly paste the code snippet without explanation

QStringQLibraryInfo::location(LibraryLocation loc){    QString ret;    if(!QLibraryInfoPrivate::configuration()) {        const char *path = 0;        switch (loc) {...        case PluginsPath:            path = QT_CONFIGURE_PLUGINS_PATH;            break;...        default:            break;        }        if (path)            ret = QString::fromLocal8Bit(path);    } else {        QString key;        QString defaultValue;        switch(loc) {        case PluginsPath:            key = QLatin1String("Plugins");            defaultValue = QLatin1String("plugins");            break;...
Cache

To accelerate plug-in loading and verification, qsettings is used to save some plug-in information.

Look at the code snippet:

QFactoryLoader::update(){    QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));    QString regkey = QString::fromLatin1("Qt Factory Cache %1.%2/%3:/%4")                             .arg((QT_VERSION & 0xff0000) >> 16)                             .arg((QT_VERSION & 0xff00) >> 8)                             .arg(QLatin1String(d->iid))                             .arg(fileName);    QStringList reg;    reg << library->lastModified;    reg += keys;    settings.setValue(regkey, reg);

And

bool QLibraryPrivate::isPlugin(QSettings *settings){...    QString regkey = QString::fromLatin1("Qt Plugin Cache %1.%2.%3/%4")                     .arg((QT_VERSION & 0xff0000) >> 16)                     .arg((QT_VERSION & 0xff00) >> 8)                     .arg(QLIBRARY_AS_DEBUG ? QLatin1String("debug") : QLatin1String("false"))                     .arg(fileName);...

Check the Registry content in Windows:

HKCU/Software/Trolltech/OrganizationDefaults/Qt Factory Cache 4.7/com.trolltech.Qt.QImageIOHandlerFactoryInterface:/F:/Qt4/plugins/imageformats/qtjpeg4.dll = 2010-09-29T14:40:30 jpeg jpg

In Linux ~ /. Config/trolltech. conf

[Qt Factory Cache 4.7]com.trolltech.Qt.QImageIOHandlerFactoryInterface:/home/Qt4/plugins/imageformats/qtjpeg4.dll = 2011-04-22T17:21:23 jpeg jpg
Reference
  • Http://doc.qt.nokia.com/latest/deployment-plugins.html

  • Http://doc.qt.nokia.com/latest/qt-conf.html

  • Http://hi.baidu.com/cyclone/blog/item/0fc7462376f208429822ed1d.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.