Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

KDE Frameworks - Part 3

KCoreAddons

QSet<QString> plugins;

QStringList dirs = QCoreApplication::libraryPaths();

for (QString dir : dirs) {
    QDirIterator it(dir +/myplugins”, QDir::Files);

    while (it.hasNext()) {
        it.next();

        if (!plugins.contains(it->fileName())) {
            plugins << it->fileName();
        }
    }
}
QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins("myplugins");
for (const KPluginMetaData &plugin : plugins) {
    QPluginLoader loader(plugin.fileName());
    MyInterface *iface = qobject_cast<MyInterface *>(plugin.instance());
}
// myinterface.h

class MyInterface : public QObject
{
    MyInterface(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args);
}

// myimpl1.cpp

#include “myinterface.h”

#include <KPluginFactory>

class MyImpl1 : public MyInterface {

MyImpl1(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
    : MyInterface(parent, metadata, args)
{
}

}

K_PLUGIN_CLASS_WITH_JSON(MyImpl1, “metadata.json”)

#include “myplugin.moc”
KPluginMetaData metaData(“myinterface/myimpl1”);
QObject *parent =;
QVariantList args =;
KPluginFactory::Result result = KPluginFactory::instantiatePlugin<MyInterface>(metaData, parent, args);

if (result) {
    MyInterface *instance = result.plugin
} else {
    qDebug() << “could not load plugin” << result.errorText
}
KUser user; // The current user

qDebug() << “Hello” << user.fullName();
QDate date =;

KFormat format;

qDebug() << format.formatRelativeDate(date, QLocale::LongFormat);
KFormat format;

qDebug() << format.formatSpelloutDuration(62005); // prints “1 minute and 2 seconds”
QFile file();

KFormat format;

// prints “1.4 MB
qDebug() << format.formatByteSize(file.size(), 1, KFormat::MetricBinaryDialect);
// prints “KDE is … awesome”
qDebug() << KStringHandler::csqueeze(“KDE is really awesome”, 19);
// prints “The Quick Brown Fox Jumped Over The Lazy Bridge.”
qDebug() << KStringHandler::capwords(“The quick brown fox jumped over the lazy bridge.);

About KDAB


4 Comments

26 - May - 2022

Julia

27 - May - 2022

Nicolas Fella

26 - May - 2022

Julia

27 - May - 2022

Nicolas Fella

NicolasFella

Nicolas Fella

Software Engineer