Qt工作笔记-QStylePlugin插件实现变化窗体背景

目录

 

关键

演示及源码


 

关键

插件端:

 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "mystyle.json")

在QStylePlugin中这个是必须的:org.qt-project.Qt.QStyleFactoryInterface"

json文件中的数据,最好与

QStyle *MyStylePlugin::create(const QString &key)
{
    if (key.toLower() == "mystyleyellow")
        return new MyStyle;
    return 0;
}

这个一样!

 

插件读取端:

QStylePlugin中可以使用QStyleFactory调用插件!

QStyleFactory::keys();可以获取key值,QStyleFactory::create()这个可以创建!

注意要放到这个文件中:

Qt工作笔记-QStylePlugin插件实现变化窗体背景_第1张图片

 

styles文件如下:

Qt工作笔记-QStylePlugin插件实现变化窗体背景_第2张图片

 

演示及源码

程序运行截图如下:

Qt工作笔记-QStylePlugin插件实现变化窗体背景_第3张图片

插件端:

Qt工作笔记-QStylePlugin插件实现变化窗体背景_第4张图片

mystyle.h

#ifndef SIMPLESTYLE_H
#define SIMPLESTYLE_H

#include 

QT_BEGIN_NAMESPACE
class QPalette;
QT_END_NAMESPACE

class MyStyle : public QProxyStyle
{
    Q_OBJECT

public:
    MyStyle() {}

    void polish(QPalette &palette) override;
};

#endif

mystyleplugin.h

#ifndef SIMPLESTYLEPLUGIN_H
#define SIMPLESTYLEPLUGIN_H

#include 

QT_BEGIN_NAMESPACE
class QStringList;
class QStyle;
QT_END_NAMESPACE

class MyStylePlugin : public QStylePlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "mystyle.json")

public:
    MyStylePlugin() {}

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

#endif

mystyle.cpp

#include "mystyle.h"
#include "mystyleplugin.h"
#include 

void MyStyle::polish(QPalette &palette)
{
    palette.setBrush(QPalette::Window, Qt::yellow);
}

mystyleplugin.cpp

#include "mystyleplugin.h"
#include "mystyle.h"
#include 

QStringList MyStylePlugin::keys() const
{
    return QStringList() << "MyStyleYellow";
}

QStyle *MyStylePlugin::create(const QString &key)
{
    if (key.toLower() == "mystyleyellow")
        return new MyStyle;
    return 0;
}

StylePluginDemo.pro

TEMPLATE = lib
CONFIG += plugin
QT += widgets

TARGET = StylePluginYellow



SOURCES += \
    mystyle.cpp \
    mystyleplugin.cpp

HEADERS  += \
    mystyle.h \
    mystyleplugin.h

EXAMPLE_FILES += mystyle.json


win32 {
    CONFIG(debug, release|debug):DESTDIR = debug/styles/
    CONFIG(release, release|debug):DESTDIR = release/styles/
} else {
    DESTDIR = ../styles/
}

EXAMPLE_FILES += simplestyle.json

json文件如下:

{
	"Keys": ["mystyleyellow"]
}

 

 

插件读取端:

Qt工作笔记-QStylePlugin插件实现变化窗体背景_第5张图片

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected slots:
    void btnClicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include 
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("CSDN IT1995");


    QStringList list = QStyleFactory::keys();

    qDebug() << list;
    QVBoxLayout *layout = new QVBoxLayout;
    for(int i = 0; i < list.size(); i++){

        if(list[i].contains("mystyle")){

            QPushButton *btn = new QPushButton(list[i]);
            layout->addWidget(btn);
            connect(btn, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));
        }
    }

    setLayout(layout);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::btnClicked()
{
    QString text = static_cast(sender())->text();
    QStyle *style = QStyleFactory::create(text);
    QApplication::setStyle(style);
}

 

你可能感兴趣的:(C/C++,Qt,C++,Qt,插件)