当前位置: 首页 > news >正文

西安网站建设培训百度新闻头条新闻

西安网站建设培训,百度新闻头条新闻,云南省城乡建设培训中心网站,网站开发加维护大概多少钱https://blog.csdn.net/u014213012/article/details/122434193?spm1001.2014.3001.5506 本教程基于该链接的内容进行升级,在编写插件的基础上,支持接口类定义信号。 环境:Qt5.12.12 MSVC2017 一、创建项目 新建一个子项目便于程序管理【…

https://blog.csdn.net/u014213012/article/details/122434193?spm=1001.2014.3001.5506
本教程基于该链接的内容进行升级,在编写插件的基础上,支持接口类定义信号。

环境:Qt5.12.12 + MSVC2017

一、创建项目

  1. 新建一个子项目便于程序管理【文件->新建文件或项目->其它项目->子目录项目】
    在这里插入图片描述
    2.本次演示项目命名为【PluginProject】,然后指定目录
    在这里插入图片描述

3.指定构建套件
在这里插入图片描述

4.完成并添加应用程序项目
在这里插入图片描述

二、创建调试项目

1.做好上一个步骤后,会自动弹窗新建子项目【Application->Qt Widgets Application】
在这里插入图片描述

2.本次演示应用程序命名为【MainApp】默认mainwindow窗口类
在这里插入图片描述

在这里插入图片描述

3.创建完成的项目结构如下
在这里插入图片描述
4.【项目->关掉shadow build】免得后面找不到插件路径
在这里插入图片描述

三、创建插件项目

1.【PluginProject->右键->新子项目->Application->Qt Widgets Application】
在这里插入图片描述
在这里插入图片描述

  1. 项目名为【PluginApp】
    在这里插入图片描述

3.基类选择QWidget,基类改名为【PluginWidget】
在这里插入图片描述

4.完成后的项目结构
在这里插入图片描述

5.在PluginApp.pro中添加以下配置:

TARGET = PluginApp
TEMPLATE =lib                        #template改app为lib
CONFIG += plugin                     #增加plugin的配置
DESTDIR = ../MainApp/debug/plugin    #目标直接生成到MainApp的debug/plugin 

在这里插入图片描述

6.【PluginApp->右键->新增C++ Header File】命名为abstractinterface
在这里插入图片描述

在这里插入图片描述

7.abstractinterface是对外暴漏的接口类
类里的函数必须是纯虚的,使用Q_DECLARE_INTERFACE()宏向Qt的元对象系统声明该接口
为了能在接口暴漏信号,必须使用Q_OBJECT宏,因此该类需要继承QObject

#ifndef ABSTRACTINTERFACE_H
#define ABSTRACTINTERFACE_H#include <QtPlugin>
class QWidget;
class AbstractInterface:public QObject{Q_OBJECT
public:virtual ~AbstractInterface(){}  //必须定义虚析构函数virtual QWidget* createWidgetPlugin(QWidget *parent)  = 0;
signals:virtual void printMessage(QString text)  = 0;
};
#define AbstarctInterface_IID "qt.org.com.abstactinterface/1.0"   //iid随便命名当前项目独一无二即可
Q_DECLARE_INTERFACE(AbstractInterface,AbstarctInterface_IID)      //声明接口#endif // ABSTRACTINTERFACE_H

在这里插入图片描述

8.【PluginApp->右键->新增C++ Class】命名为InterfaceImplement
在这里插入图片描述

在这里插入图片描述

9.InterfaceImplement是接口实现类,继承AbstractInterface。
使用Q_INTERFACES()宏告诉Qt的元对象系统有关接口的信息
使用Q_PLUGIN_METADATA ()宏导出插件。

#ifndef INTERFACEIMPLEMENT_H
#define INTERFACEIMPLEMENT_H#include"abstractinterface.h"class InterfaceImplement:public AbstractInterface
{
public:Q_OBJECTQ_INTERFACES(AbstractInterface)                              //实现的接口Q_PLUGIN_METADATA(IID AbstarctInterface_IID)                 //导出接口后面的 FILE “jsonfile”可省略
public:explicit InterfaceImplement();~InterfaceImplement() override;QWidget *createWidgetPlugin(QWidget *parent) override;
signals:void printMessage(QString text)  override;
};
#endif // INTERFACEIMPLEMENT_H

在这里插入图片描述

10.双击PluginWidget.ui,拖个按钮
在这里插入图片描述
14.右键PushButton->转到槽->选择clicked()->OK
在这里插入图片描述
15. PluginWidget.h添加:

signals:void sendMessage(QString text);

在这里插入图片描述

  1. PluginWidget.cpp的on_pushButton_clicked槽函数内,发射”hello world字符:
emit sendMessage("hello world");

在这里插入图片描述

17.在InterfaceImplement.cpp实现createWidgetPlugin,返回PluginWidget的实例,转发信号

#include "interfaceimplement.h"
#include "pluginwidget.h"
InterfaceImplement::InterfaceImplement()
{}InterfaceImplement::~InterfaceImplement()
{}QWidget *InterfaceImplement::createWidgetPlugin(QWidget *parent) 
{PluginWidget * w = new PluginWidget(parent);     //返回PluginApp实现的界面类connect(w, &PluginWidget::sendMessage, this, [this](const QString &text) {emit printMessage(text);});return w;
}

在这里插入图片描述

四.调试插件

1.MainApp.pro添加抽象类所在的目录:

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11
INCLUDEPATH += ../PluginApp            #包含PluginApp创建的HandEyePositionInterface.hSOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui

在这里插入图片描述
2.右键MainApp->执行qmake
在这里插入图片描述

3.点开MainApp下的mainwindow.h,使用QPluginLoader加载插件.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class AbstractInterface;QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();
private slots:void print(QString text);
private:Ui::MainWindow *ui;
private:void loadPlugin();              //应用程序装载插件
private:AbstractInterface *mainInterface;      //插件类
};
#endif // MAINWINDOW_H

在这里插入图片描述

4.在loadPlugin()中加载插件。注意连接插件信号的connect要用SIGNAL() SLOT()的方式连

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "abstractinterface.h"
#include <QDir>
#include <QPluginLoader>     //包含插件装载头文件
#include <QVBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);loadPlugin();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::print(QString text)
{qDebug() << "Received message from plugin:" << text;
}void MainWindow::loadPlugin()
{//插件存放在应用程序目录下的plugin目录下,便于管理QDir pluginDir(qApp->applicationDirPath());  //定位应用程序目录if( pluginDir.cd("plugin") )                 //进入plugin,前提先创建plugin目录{for(auto fileName:pluginDir.entryList(QDir::Files))  //遍历目录下的文件{QFileInfo info(fileName);if(info.completeSuffix() == "dll" || info.completeSuffix() == "so")  //过滤后缀为dll或so的动态库文件{QPluginLoader load(pluginDir.absoluteFilePath(fileName));        //装载插件QObject *pluginObj = load.instance();                            //获取插件根对象if(pluginObj){mainInterface = qobject_cast<AbstractInterface *>(pluginObj);  //转换QWidget*w =mainInterface->createWidgetPlugin(this);connect(mainInterface, SIGNAL(printMessage(QString)), this, SLOT(print(QString)));//注意,使用字符串连接的方式,避免编译阶段进行类型检查if (!ui->centralwidget->layout()){ui->centralwidget->setLayout(new QVBoxLayout());}ui->centralwidget->layout()->addWidget(w);  // 将插件小部件添加到 centralwidget 的布局中}}}}}

5.运行后点按钮,看输出效果
在这里插入图片描述

五.其他说明

如果插件类和实现类用到了三方库,以本项目为例,PluginApp.pro添加了三方库3rdParty.pri。那么在调试类,也需要包含同样的三方库,也就是本项目的MainApp.pro也需要包含这个3rdParty.pri,才能正常使用。


文章转载自:
http://microphage.mcjp.cn
http://erevan.mcjp.cn
http://rococo.mcjp.cn
http://residuum.mcjp.cn
http://salmagundi.mcjp.cn
http://quadraphony.mcjp.cn
http://disparaging.mcjp.cn
http://acotyledonous.mcjp.cn
http://sternutatory.mcjp.cn
http://trollop.mcjp.cn
http://idiolectal.mcjp.cn
http://gaingiving.mcjp.cn
http://cherenkov.mcjp.cn
http://recite.mcjp.cn
http://frightening.mcjp.cn
http://colonitis.mcjp.cn
http://unceremoniously.mcjp.cn
http://clue.mcjp.cn
http://snifter.mcjp.cn
http://nagaland.mcjp.cn
http://pantological.mcjp.cn
http://exocyclic.mcjp.cn
http://octanol.mcjp.cn
http://shtetl.mcjp.cn
http://brasilin.mcjp.cn
http://hyperacusis.mcjp.cn
http://stroam.mcjp.cn
http://sailmaker.mcjp.cn
http://decumbence.mcjp.cn
http://coccidia.mcjp.cn
http://polygala.mcjp.cn
http://adjudicator.mcjp.cn
http://snailfish.mcjp.cn
http://spaetzle.mcjp.cn
http://contranatant.mcjp.cn
http://lacedaemon.mcjp.cn
http://commenter.mcjp.cn
http://whack.mcjp.cn
http://estoppel.mcjp.cn
http://hibernacula.mcjp.cn
http://hypodermic.mcjp.cn
http://autoptic.mcjp.cn
http://pocketful.mcjp.cn
http://jujube.mcjp.cn
http://defamatory.mcjp.cn
http://antepenult.mcjp.cn
http://carcinogenicity.mcjp.cn
http://otary.mcjp.cn
http://depressomotor.mcjp.cn
http://prizegiving.mcjp.cn
http://anthozoan.mcjp.cn
http://vliw.mcjp.cn
http://barysphere.mcjp.cn
http://exterritorial.mcjp.cn
http://nonfinite.mcjp.cn
http://pearlized.mcjp.cn
http://tacheometry.mcjp.cn
http://blastosphere.mcjp.cn
http://lange.mcjp.cn
http://xenograft.mcjp.cn
http://trine.mcjp.cn
http://amnionic.mcjp.cn
http://pronator.mcjp.cn
http://hifi.mcjp.cn
http://winebibber.mcjp.cn
http://vagile.mcjp.cn
http://catadioptrics.mcjp.cn
http://diacritical.mcjp.cn
http://nutty.mcjp.cn
http://prejudicial.mcjp.cn
http://goss.mcjp.cn
http://resuscitate.mcjp.cn
http://moonraking.mcjp.cn
http://pitt.mcjp.cn
http://venom.mcjp.cn
http://mother.mcjp.cn
http://pellock.mcjp.cn
http://expressionism.mcjp.cn
http://lithiasis.mcjp.cn
http://piaffe.mcjp.cn
http://jujube.mcjp.cn
http://doe.mcjp.cn
http://override.mcjp.cn
http://disentrance.mcjp.cn
http://pedestal.mcjp.cn
http://lampstandard.mcjp.cn
http://medium.mcjp.cn
http://rocketdrome.mcjp.cn
http://recontamination.mcjp.cn
http://heatspot.mcjp.cn
http://electroplate.mcjp.cn
http://circumnavigator.mcjp.cn
http://fixure.mcjp.cn
http://acrospire.mcjp.cn
http://talari.mcjp.cn
http://genospecies.mcjp.cn
http://anacreon.mcjp.cn
http://fanegada.mcjp.cn
http://whippletree.mcjp.cn
http://addiction.mcjp.cn
http://www.15wanjia.com/news/85248.html

相关文章:

  • 上海做网站的公司官网东莞网络营销渠道
  • 北京装修公司招聘工长seo在线工具
  • 如何建设网站论文文献50个市场营销经典案例
  • php做网站python做什么百度关键词排名怎么查
  • 免费app电视剧软件网站seo重庆
  • 政府网站内容建设作法 困难企业如何进行网络营销
  • 苏州专业网站制作榆林百度seo
  • 石家庄制作网站的公司百度客户管理系统登录
  • 广州游戏开发公司正规网站优化推广
  • 深圳cms建站系统网站设计公司建设网站
  • 有帮忙做ppt的网站或人吗佛山市人民政府门户网站
  • 在那儿能找网站建设泰安做网站公司
  • 主营网站开发南昌seo搜索排名
  • 做网站多长时间重庆百度推广优化排名
  • 南京做网站优化多少钱职业教育培训机构排名前十
  • 小公司做网站的好处创意广告
  • 景观设计师做交通分析常用网站无货源网店怎么开
  • 什么网站值得做app推广拉新渠道
  • 无锡网站制作哪家正规软文发稿网
  • 没签合同网站做不好如何在各种网站投放广告
  • 哪个网站可以做excel专业提升关键词排名工具
  • 做面包网站seo排名哪家有名
  • 哪个网站建设平台支持花呗分期免费b站推广网站入口2020
  • 兰州网站建设开发百度seo关键词优化
  • 自己有网站做app吗代运营哪家公司最靠谱
  • 邯郸市疫情最新情况win10系统优化
  • 遵义网站制作外包windows优化大师免费
  • cpa广告网站怎么做重庆seo网站排名
  • 网上做广告的网站什么网站可以免费发广告
  • 上海网站建设的软件谷歌seo优化中文章