KParts::PartLoader

KParts::PartLoader Namespace Reference

Functions

template<class T >
static T * createPartInstanceForMimeType (const QString &mimeType, QWidget *parentWidget=nullptr, QObject *parent=nullptr, QString *error=nullptr)
 
template<typename T >
static KPluginFactory::Result< T > instantiatePart (const KPluginMetaData &data, QWidget *parentWidget=nullptr, QObject *parent=nullptr, const QVariantList &args={})
 
template<class T >
static KPluginFactory::Result< T > instantiatePartForMimeType (const QString &mimeType, QWidget *parentWidget=nullptr, QObject *parent=nullptr, const QVariantList &args={})
 
KPARTS_EXPORT QList< KPluginMetaDatapartsForMimeType (const QString &mimeType)
 

Detailed Description

Helper methods for locating and loading parts.

This is based upon KPluginFactory, but it takes care of querying by mimetype, sorting the available parts by builtin preference and by user preference.

Since
5.69

Function Documentation

◆ createPartInstanceForMimeType()

template<class T >
static T* KParts::PartLoader::createPartInstanceForMimeType ( const QString mimeType,
QWidget parentWidget = nullptr,
QObject parent = nullptr,
QString error = nullptr 
)
static

Use this method to create a KParts part.

It will try to create an object which inherits T.

Example:

QString errorString;
m_part = KParts::PartLoader::createPartInstanceForMimeType<KParts::ReadOnlyPart>(
mimeType, this, this, &errorString);
if (m_part) {
layout->addWidget(m_part->widget()); // Integrate the widget
createGUI(m_part); // Integrate the actions
m_part->openUrl(url);
} else {
qWarning() << errorString;
}
Template Parameters
TThe interface for which an object should be created. The object will inherit T.
Parameters
mimeTypeThe mimetype for which we need a KParts.
parentWidgetThe parent widget for the part's widget.
parentThe parent of the part.
errorOptional output parameter, it will be set to the error string, if any.
Returns
A pointer to the created object is returned, or nullptr if an error occurred.
Since
5.69

Definition at line 193 of file partloader.h.

◆ instantiatePart()

template<typename T >
static KPluginFactory::Result<T> KParts::PartLoader::instantiatePart ( const KPluginMetaData data,
QWidget parentWidget = nullptr,
QObject parent = nullptr,
const QVariantList &  args = {} 
)
static

Attempts to create a KPart from the given metadata.

KPluginFactory::Result<MyPart> result = KParts::PartLoader::instantiatePart<MyPart>(metaData, parentWidget, parent, args);
if (result) {
// result.plugin is valid and can be accessed
} else {
// result contains information about the error
}

If there is no extra error handling needed the plugin can be directly accessed and checked if it is a nullptr

if (auto plugin = KParts::PartLoader::instantiatePart<MyPart>(metaData, parentWidget, parent, args).plugin) {
// The plugin is valid and can be accessed
}
Parameters
dataKPluginMetaData from which the plugin should be loaded
parentWidgetThe parent widget
parentThe parent object
argsA list of arguments to be passed to the part
Returns
Result object which contains the plugin instance and potentially error information
Since
5.100

Definition at line 90 of file partloader.h.

◆ instantiatePartForMimeType()

template<class T >
static KPluginFactory::Result<T> KParts::PartLoader::instantiatePartForMimeType ( const QString mimeType,
QWidget parentWidget = nullptr,
QObject parent = nullptr,
const QVariantList &  args = {} 
)
static

Use this method to create a KParts part.

It will try to create an object which inherits T.

KPluginFactory::Result<KParts::ReadOnlyPart> result = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(mimeType, parentWidget, parent,
args);
if (result) {
// result.plugin is valid and can be accessed
} else {
// result contains information about the error
}

If there is no extra error handling needed the plugin can be directly accessed and checked if it is a nullptr

if (auto plugin = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(mimeType, parentWidget, parent, args).plugin) {
// The plugin is valid and can be accessed
}
Template Parameters
TThe interface for which an object should be created. The object will inherit T.
Parameters
mimeTypeThe mimetype for which we need a KParts.
parentWidgetThe parent widget for the part's widget.
parentThe parent of the part.
Returns
Result object which contains the plugin instance and potentially error information
Since
5.100

Definition at line 139 of file partloader.h.

◆ partsForMimeType()

QList< KPluginMetaData > KParts::PartLoader::partsForMimeType ( const QString mimeType)

Locate all available KParts for a mimetype.

Returns
a list of plugin metadata, sorted by preference. This takes care both of the builtin preference (set by developers) and of user preference (stored in mimeapps.list).

This uses KPluginMetaData::findPlugins, i.e. it requires the parts to provide the metadata as JSON embedded into the plugin. Until KF6, however, it also supports .desktop files as a fallback solution.

To load a part from one of the KPluginMetaData instances returned here, use instantiatePart()

Since
5.69

Definition at line 71 of file partloader.cpp.

This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jun 4 2023 03:54:22 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.