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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/86602436

目录

 

关键

演示及源码


 

关键

插件端:

 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()这个可以创建!

注意要放到这个文件中:

styles文件如下:

演示及源码

程序运行截图如下:

插件端:

mystyle.h

#ifndef SIMPLESTYLE_H
#define SIMPLESTYLE_H

#include <QProxyStyle>

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 <QStylePlugin>

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 <QtWidgets>

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

mystyleplugin.cpp

#include "mystyleplugin.h"
#include "mystyle.h"
#include <QtWidgets>

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"]
}

插件读取端:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

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 <QApplication>

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 <QDebug>
#include <QStyleFactory>
#include <QStyle>
#include <QVBoxLayout>
#include <QPushButton>

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<QPushButton*>(sender())->text();
    QStyle *style = QStyleFactory::create(text);
    QApplication::setStyle(style);
}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/86602436