关键代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringListModel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// C++基础数据类型列表
QStringList types;
types << "bool"
<< "char"
<< "signed char"
<< "unsigned char"
<< "short"
<< "short int"
<< "signed short"
<< "signed short int"
<< "unsigned short"
<< "unsigned short int"
<< "int"
<< "signed"
<< "unsigned"
<< "long"
<< "long int"
<< "signed long"
<< "signed long int"
<< "unsigned long"
<< "unsigned long int"
<< "long long"
<< "long long int"
<< "signed long long"
<< "signed long long int"
<< "unsigned long long"
<< "unsigned long long int"
<< "int8_t"
<< "uint8_t"
<< "int16_t"
<< "uint16_t"
<< "int32_t"
<< "uint32_t"
<< "wchar_t"
<< "int64_t"
<< "uint64_t"
<< "float"
<< "double"
<< "long double";
// 添加类型到组合框
foreach (const QString &type, types) {
ui->cbo_type->addItem(type);
}
// C++基础数据类型列表
QStringList decorations;
decorations << "T" // 普通类型
<< "T*" // 指针类型
<< "T&" // 引用类型
<< "T&&" // 右值引用类型
<< "T const*" // 常量指针类型
<< "const T*" // 指向常量的指针类型
<< "T const&" // 常量引用类型
<< "const T&" // 同上
<< "T const&&" // 常量右值引用类型
<< "const T&&" // 同上
<< "T volatile*" // volatile指针类型
<< "volatile T*" // 指向volatile类型的指针
<< "T const volatile*" // 常量volatile指针类型
<< "const volatile T*" // 指向常量volatile类型的指针
<< "T[]"; // 数组类型
// 添加类型到组合框
foreach (const QString &decoration, decorations) {
ui->cbo_decoration->addItem(decoration);
}
QStringListModel* model = new QStringListModel(this);
model->setStringList(QStringList());
ui->lst_interface->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_add_clicked()
{
auto type = ui->cbo_type->currentText();
auto decoration = ui->cbo_decoration->currentText();
auto var_name = ui->edt_member_name->text();
bool hasSet = ui->chk_set->isChecked();
bool hasGet = ui->chk_get->isChecked();
if(hasSet) { // setName
QString returnType = decoration;
returnType.replace("T", type);
QString lineFunc;
auto newName = var_name;
newName.replace(0, 1, newName.at(0).toUpper()); // 驼峰命名
lineFunc += "void"; lineFunc += " "; lineFunc += "set"; lineFunc += newName; lineFunc += "("; lineFunc += returnType; lineFunc += " "; lineFunc += var_name; lineFunc += ")";
QStringListModel* model = (QStringListModel*)ui->lst_interface->model();
auto lineNo = model->rowCount();
model->insertRow(lineNo);
model->setData(model->index(lineNo), lineFunc);
}
if(hasGet) { // getName
QString returnType = decoration;
returnType.replace("T", type);
QString lineFunc;
auto newName = var_name;
newName.replace(0, 1, newName.at(0).toUpper()); // 驼峰命名
lineFunc += returnType; lineFunc += " "; lineFunc += "get"; lineFunc += newName; lineFunc += "("; lineFunc += ")"; lineFunc += " "; lineFunc += "const";
QStringListModel* model = (QStringListModel*)ui->lst_interface->model();
auto lineNo = model->rowCount();
model->insertRow(lineNo);
model->setData(model->index(lineNo), lineFunc);
}
}
void MainWindow::on_btn_gen_clicked()
{
auto className = ui->edt_interface_name->text();
className.replace(0, 1, className.at(0).toUpper()); // 驼峰命名
QStringList classInterface;
QString lineDefineClassInterface;
lineDefineClassInterface += "class"; lineDefineClassInterface += " "; lineDefineClassInterface += className; lineDefineClassInterface += " "; lineDefineClassInterface += "{";
classInterface << lineDefineClassInterface;
QString fieldKey;
fieldKey += "public:";
classInterface << fieldKey;
QString dector;
dector += "\t"; dector += "~"; dector += className; dector += "("; dector += ")"; dector += "=default"; dector += ";";
classInterface << dector;
auto members_count = ui->lst_interface->model()->rowCount();
for (int var = 0; var < members_count; ++var) {
QString lineFunc = ui->lst_interface->model()->index(var, 0).data().toString();
QString lineMember;
lineMember += "\t"; lineMember += "virtual"; lineMember += " "; lineMember += lineFunc; lineMember += "=0"; lineMember += ";";
classInterface << lineMember;
}
classInterface << "};";
QString fileStream = classInterface.join("\r\n");
ui->edt_output->setPlaceholderText(fileStream);
}
效果
创作不易,小小的支持一下吧!