IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QStylePlugin Class

The QStylePlugin class provides an abstract base for custom QStyle plugins.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QStylePlugin Class

  • Header: QStylePlugin

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Widgets)

    target_link_libraries(mytarget PRIVATE Qt6::Widgets)

  • qmake: QT += widgets

  • Inherits: QObject

  • Group: QStylePlugin is part of plugins

Detailed Description

QStylePlugin is a simple plugin interface that makes it easy to create custom styles that can be loaded dynamically into applications using the QStyleFactory class.

Writing a style plugin is achieved by subclassing this base class, reimplementing the pure virtual create() function, and exporting the class using the Q_PLUGIN_METADATA() macro.

 
Sélectionnez
class MyStylePlugin : public QStylePlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "mystyleplugin.json")
public:
    MyStylePlugin(QObject *parent = nullptr);

    QStyle *create(const QString &key) override;
};

The json metadata file mystyleplugin.json for the plugin needs to contain information about the names of the styles the plugins supports as follows:

 
Sélectionnez
{ "Keys": [ "Rocket", "Starbuster" ] }

See How to Create Qt Plugins for details.

See Also

See also QStyleFactory, QStyle

Member Function Documentation

 

[explicit] QStylePlugin::QStylePlugin(QObject *parent = nullptr)

Constructs a style plugin with the given parent.

Note that this constructor is invoked automatically by the moc generated code that exports the plugin, so there is no need for calling it explicitly.

[virtual] QStylePlugin::~QStylePlugin()

Destroys the style plugin.

Note that Qt destroys a plugin automatically when it is no longer used, so there is no need for calling the destructor explicitly.

[pure virtual] QStyle *QStylePlugin::create(const QString &key)

Creates and returns a QStyle object for the given style key. If a plugin cannot create a style, it should return 0 instead.

The style key is usually the class name of the required style. Note that the keys are case insensitive. For example:

 
Sélectionnez
QStyle *MyStylePlugin::create(const QString &key)
{
    QString lcKey = key.toLower();
    if (lcKey == "rocket") {
        return new RocketStyle;
    } else if (lcKey == "starbuster") {
        return new StarBusterStyle;
    }
    return nullptr;
}

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+