VS2017+QT一个dll中包含多个插件

在https://blog.csdn.net/baidu_33662202/article/details/89052254中我已经写过QT如何自定义插件。自定义插件的结果如下图

VS2017+QT一个dll中包含多个插件_第1张图片

这里先介绍一个类QDesignerCustomWidgetInterface,这个类是QT用于自定义插件的接口,它的函数如下

VS2017+QT一个dll中包含多个插件_第2张图片

 group函数返回的是所在组的名字,比如图中的My Plugins;name函数返回的是插件的名字,比如图中的QtDesignerWidget

如果我们想要实现图中的自定义插件效果,即一个组下面有多个插件;我们可以建立多个dll,让她们返回同一个组名(group函数返回一致);但是,这就麻烦了,所以我们需要把多个插件集成到一个dll中。

1.建立一个qt class library

VS2017+QT一个dll中包含多个插件_第3张图片

2. 建立四个类,CustomButton1,CustomButton1Plugin,CustomButton2,CustomButton2Plugin

 右键项目属性,add->add qt class,其中CustomButton1和CustomButton2继承自QWidget,CustomButtonPlugin1和CustomButtonPlugin2继承自QObject和QDesignerCustomWidgetInterface

VS2017+QT一个dll中包含多个插件_第4张图片

 CustomButton1类

//h文件

#pragma once

#include 

class CustomButton1 : public QWidget
{
	Q_OBJECT

public:
	CustomButton1(QWidget *parent);
	~CustomButton1();
};


//cpp文件
#include "CustomButton1.h"
#include
#include 
CustomButton1::CustomButton1(QWidget *parent)
	: QWidget(parent)
{
	QVBoxLayout *layout = new QVBoxLayout(this);
	QPushButton *button = new QPushButton(this);
	layout->addWidget(button);
	button->setText("button1");
}

CustomButton1::~CustomButton1()
{
}

当我们在cpp文件中加入qpushbutton 和 qvboxlayout头文件时,可能会报错,这是因为我们没有引入相应的库,比如pushbuton,vboxlayout属于QGUI库,QWidget输入QWidgets库。添加方法为, 选择qt vs tools ->qt project settings,我们勾选相应的qt modules

VS2017+QT一个dll中包含多个插件_第5张图片

 

 CustomButton1Plugin类

//h文件

#pragma once

#include 
#include "QtDesigner/QDesignerCustomWidgetInterface"
class CustomButton1Plugin : public QObject,public QDesignerCustomWidgetInterface
{
	Q_OBJECT
	Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
	CustomButton1Plugin(QObject *parent);
	~CustomButton1Plugin();
	bool isContainer() const;
	bool isInitialized() const;
	QIcon icon() const;
	QString domXml() const;
	QString group() const;
	QString includeFile() const;
	QString name() const;
	QString toolTip() const;
	QString whatsThis() const;
	QWidget *createWidget(QWidget *parent);
	void initialize(QDesignerFormEditorInterface *core);
private:
	bool initialized;
};


//cpp文件
#include "CustomButton1Plugin.h"
#include "CustomButton1.h"
#include 
CustomButton1Plugin::CustomButton1Plugin(QObject *parent)
	: QObject(parent)
{
	initialized = false;
}

CustomButton1Plugin::~CustomButton1Plugin()
{
}



void CustomButton1Plugin::initialize(QDesignerFormEditorInterface * /*core*/)
{
	if (initialized)
		return;

	initialized = true;
}

bool CustomButton1Plugin::isInitialized() const
{
	return initialized;
}

QWidget *CustomButton1Plugin::createWidget(QWidget *parent)
{
	return new CustomButton1(parent);
}

QString CustomButton1Plugin::name() const
{
	return "CustomButton1";
}

QString CustomButton1Plugin::group() const
{
	return "My Plugins";
}

QIcon CustomButton1Plugin::icon() const
{
	return QIcon();
}

QString CustomButton1Plugin::toolTip() const
{
	return QString();
}

QString CustomButton1Plugin::whatsThis() const
{
	return QString();
}

bool CustomButton1Plugin::isContainer() const
{
	return false;
}

QString CustomButton1Plugin::domXml() const
{
	return "\n"
		"\n";
}

QString CustomButton1Plugin::includeFile() const
{
	return "CustomButton1.h";
}

 CustomButton2类

//h文件

#pragma once

#include 

class CustomButton2 : public QWidget
{
	Q_OBJECT

public:
	CustomButton2(QWidget *parent);
	~CustomButton2();
};


//cpp文件

#include "CustomButton2.h"
#include
#include 
CustomButton2::CustomButton2(QWidget *parent)
	: QWidget(parent)
{
	QVBoxLayout *layout = new QVBoxLayout(this);
	QPushButton *button = new QPushButton(this);
	layout->addWidget(button);
	button->setText("button2");
}

CustomButton2::~CustomButton2()
{
}

 CustomButton2Plugin类

//.h
#pragma once

#include 
#include "QtDesigner/QDesignerCustomWidgetInterface"
class CustomButton2Plugin : public QObject,public QDesignerCustomWidgetInterface
{
	Q_OBJECT
	Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
	CustomButton2Plugin(QObject *parent);
	~CustomButton2Plugin();
	bool isContainer() const;
	bool isInitialized() const;
	QIcon icon() const;
	QString domXml() const;
	QString group() const;
	QString includeFile() const;
	QString name() const;
	QString toolTip() const;
	QString whatsThis() const;
	QWidget *createWidget(QWidget *parent);
	void initialize(QDesignerFormEditorInterface *core);
private:
	bool initialized;


};

//cpp
#include "CustomButton2Plugin.h"
#include "CustomButton2.h"
#include 
CustomButton2Plugin::CustomButton2Plugin(QObject *parent)
	: QObject(parent)
{
	initialized = false;
}

CustomButton2Plugin::~CustomButton2Plugin()
{
}



void CustomButton2Plugin::initialize(QDesignerFormEditorInterface * /*core*/)
{
	if (initialized)
		return;

	initialized = true;
}

bool CustomButton2Plugin::isInitialized() const
{
	return initialized;
}

QWidget *CustomButton2Plugin::createWidget(QWidget *parent)
{
	return new CustomButton2(parent);
}

QString CustomButton2Plugin::name() const
{
	return "CustomButton2";
}

QString CustomButton2Plugin::group() const
{
	return "My Plugins";
}

QIcon CustomButton2Plugin::icon() const
{
	return QIcon();
}

QString CustomButton2Plugin::toolTip() const
{
	return QString();
}

QString CustomButton2Plugin::whatsThis() const
{
	return QString();
}

bool CustomButton2Plugin::isContainer() const
{
	return false;
}

QString CustomButton2Plugin::domXml() const
{
	return "\n"
		"\n";
}

QString CustomButton2Plugin::includeFile() const
{
	return "CustomButton2.h";
}

3.修改MultiPlugins类

让MultiPlugins继承 QDesignerCustomWidgetCollectionInterface类,QDesignerCustomWidgetCollectionInterface是qt用于把多个插件包含到一个dll的接口

//h文件
#pragma once

#include "multiplugins_global.h"
#include 
class MULTIPLUGINS_EXPORT MultiPlugins
	:public QObject,public QDesignerCustomWidgetCollectionInterface
{
	Q_OBJECT
	Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface")
	Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
public:
	MultiPlugins(QObject *parent = 0);
	QList customWidgets() const override;

private:
	QList widgets;
};

//cpp

#include "MultiPlugins.h"
#include "CustomButton1Plugin.h"
#include "CustomButton2Plugin.h"
MultiPlugins::MultiPlugins(QObject *parent)
	: QObject(parent)
{
	widgets.append(new CustomButton1Plugin(this));
	widgets.append(new CustomButton2Plugin(this));

}


QList MultiPlugins::customWidgets() const
{
	return widgets;
}

4.生成release版本

VS2017+QT一个dll中包含多个插件_第6张图片

5.把multiplugins.dll复制到qt designer下 

VS2017+QT一个dll中包含多个插件_第7张图片

 qt designer中的效果

 

可以在qt designer中拖拽使用我们的插件,还必须在项目中包含插件的头文件和源文件。 

积分用不完的朋友:https://download.csdn.net/download/baidu_33662202/11099599

你可能感兴趣的:(QT,C++)