简述
将插件接口做好后,就可以开始插件的开发了。由于在工作中,经常要用到正则表达式,所以,这第一个插件就先写一个正则表达式测试工具好了。还是在之前的多工程目录下建立一个插件子工程。
工程文件
添加元数据的json 文件:
DISTFILES += RegExp.json
目标文件放置目录:DESTDIR = ../bin/plugins
依赖路径:DEPENDPATH += ../bin
添加头文件搜索路径:INCLUDEPATH += $$PWD/../PluginWidget
包含插件接口库:LIBS += -L../bin/ -lPluginWidget
添加头文件搜索路径:INCLUDEPATH += \$\$PWD/../MyShareLibrary
包含公共库:LIBS += -L../bin/ -lMyShareLibrary
QT += core gui widgets
TARGET = RegExp
TEMPLATE = lib
CONFIG += plugin
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
regexp.cpp
HEADERS += \
regexp.h
FORMS += \
regexp.ui
DISTFILES += RegExp.json
DESTDIR = ../bin/plugins
DEPENDPATH += ../bin
INCLUDEPATH += $$PWD/../PluginWidget
LIBS += -L../bin/ -lPluginWidget
INCLUDEPATH += $$PWD/../MyShareLibrary
LIBS += -L../bin/ -lMyShareLibrary
这里,我们将插件生成在 ../bin/plugins
目录中。
头文件
Q_PLUGIN_METADATA
宏包含插件的 IID,并指向一个包含插件元数据的 Json 文件。该 Json 文件会被编译到插件中,无需安装。Q_INTERFACES
宏用于告诉 Qt 该类实现的接口。
#ifndef REGEXP_H
#define REGEXP_H
#include <QWidget>
#include "pluginwidget.h"
namespace Ui {
class RegExp;
}
class RegExp : public PluginWidget
{
Q_OBJECT
Q_PLUGIN_METADATA(IID PluginWidget_iid FILE "RegExp.json")
Q_INTERFACES(PluginWidget)
public:
explicit RegExp(QWidget *parent = 0);
~RegExp();
virtual RegExp *newObj() Q_DECL_OVERRIDE;
private slots:
void slot_regExp();
void on_openFile_clicked();
private:
Ui::RegExp *ui;
};
#endif // REGEXP_H
源文件
#include "regexp.h"
#include "ui_regexp.h"
#include "myassistant.h"
#include <QDir>
#include <QDebug>
#include <QClipboard>
#include <QFileDialog>
#include <QRegularExpression>
namespace {
const QString configPath = "./Configuration/RegExp/";
QString patternToCode(QString pattern)
{
pattern.replace(QStringLiteral("\\"), QStringLiteral("\\\\"));
pattern.replace(QStringLiteral("\""), QStringLiteral("\\\""));
return pattern;
}
void writeConfigFile(QString txt, QString name)
{
My::writeFile(configPath + name, txt.toLocal8Bit());
}
QString readConfigFile(QString name)
{
return My::readFile(configPath + name);
}
}//namespace end
RegExp::RegExp(QWidget *parent) :
PluginWidget(parent),
ui(new Ui::RegExp)
{
ui->setupUi(this);
connect(ui->refresh, &QToolButton::clicked, this, &RegExp::slot_regExp);
connect(ui->patternLineEdit, &QLineEdit::textChanged, this, &RegExp::slot_regExp);
connect(ui->subjectPlainTextEdit, &QPlainTextEdit::textChanged, this, &RegExp::slot_regExp);
connect(ui->clearSubjectBtn, &QToolButton::clicked, ui->subjectPlainTextEdit, &QPlainTextEdit::clear);
connect(ui->cpPatternBtn, &QToolButton::clicked, [&]{
QGuiApplication::clipboard()->setText(ui->patternLineEdit->text());
});
connect(ui->cpEscapedPatternBtn, &QToolButton::clicked, [&]{
QGuiApplication::clipboard()->setText(ui->escapedPatternLabel->text());
});
ui->patternLineEdit->setText(readConfigFile("pattern.txt"));
ui->subjectPlainTextEdit->setPlainText(readConfigFile("subject.txt"));
}
RegExp::~RegExp()
{
writeConfigFile(ui->patternLineEdit->text(),"pattern.txt");
writeConfigFile(ui->subjectPlainTextEdit->toPlainText(),"subject.txt");
delete ui;
}
RegExp *RegExp::newObj()
{
static bool flag = false;
if(flag) {
return new RegExp();
} else {
flag = true;
return this;
}
}
void RegExp::slot_regExp()
{
QString sourceData = ui->subjectPlainTextEdit->toPlainText();
QString pattern = ui->patternLineEdit->text();
ui->matchResult->clear();
ui->countLab->setText(QString("%1").arg(0));
ui->escapedPatternLabel->setText(patternToCode(pattern));
if(sourceData.isEmpty() || pattern.isEmpty()) {
return;
}
QRegularExpression rx(pattern);
QStringList texts = rx.match(sourceData).capturedTexts();
if(texts.isEmpty()) {
ui->patternLineEdit->setStyleSheet("color: rgb(255, 0, 0);");
return ;
} else {
ui->patternLineEdit->setStyleSheet("color: rgb(0, 0, 0);");
}
int count = 0;
for(QString text : texts) {
ui->matchResult->appendPlainText(QString("第 %1 个匹配:").arg(count));
ui->matchResult->appendPlainText(text);
ui->matchResult->appendPlainText("");
count++;
}
ui->countLab->setText(QString("%1").arg(count));
}
void RegExp::on_openFile_clicked()
{
QString path = QFileDialog::getOpenFileName(this);
QFile file(path);
if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {
ui->subjectPlainTextEdit->setPlainText(file.readAll());
}
file.close();
}