QT 实现对SQLite数据库的管理

界面:

QT 实现对SQLite数据库的管理_第1张图片QT 实现对SQLite数据库的管理_第2张图片

QT 实现对SQLite数据库的管理_第3张图片

链接:https://pan.baidu.com/s/1DnwMjUlqja6n6D1b9-ZySg 
提取码:lwzz

EmbeddedSystem.h

#pragma once

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "ui_EmbeddedSystem.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

struct Table
{
	QString table_name;
	QString primaryKey;
	QStringList col_name;
	QStringList col_type;
	int row_count = 0;
	vector> data;
};

class SQLite;
QString get_errorMsg(SQLite&);

	class SQLite : public QWidget
	{
		Q_OBJECT
	public:
		SQLite();
		~SQLite();
		bool bulidTables();
		const QStringList& get_curColname();			
		int get_curColcount();
		int get_curRowcount();
	
		QString get_curTablename();
		QString get_primaryKey();
		QString get_colType(const QString&);
		QString get_dataAt(int, int);
		const QStringList& get_colTypes();
		vector get_Index(const QString&, const QString&);

		const vector get_row(int);
		const QStringList get_Tablesname();
		const vector>& get_cur_Tabledata();
	
		QString toFormal(const QStringList&, QStringList&);				//规范化数据
		QString toFormal(const QString&, QString&);

		bool newTble(const QStringList&,const QStringList&,int);		//新建表
		void changeTable(const QString&);								//变更当前表
		int searchTable(const QString&);								//根据表名查找表索引
		bool removeTable(const QString&);								//根据表名删除表

		bool openDatabase(const QString&);								//打开数据库
		bool insertValues(const QString&,  QStringList&);				//插入值
		const vector searchValues(const QString&,  QString&);		//查找值
		bool deleteValues(const QString&,  QString&);					//删除值
		bool changeValues(const int, const int, QString&);				//修改值
		void clearThis();						
	signals:
		void need_update();
	private:
		friend QString get_errorMsg(SQLite&);							//获取错误信息
		QSqlDatabase database;											//数据库
		QSqlQuery* query;
		vector Tables;											//存储表
		int cur_Index;													//当前表的索引
	};


class EmbeddedSystem : public QMainWindow
{
	Q_OBJECT
public:
	EmbeddedSystem(QWidget *parent = Q_NULLPTR);
	~EmbeddedSystem();
public slots:
	void Update();						//根据当前选择的表来更新显示的内容
	void Insert();						//插入操作
	void Delete();						//删除操作
	void Search();						//查找操作
	void Drop();						//删除操作
	void Open();						//打开操作

	void On_changeTables(QAction *a);	//更改表
	void editCell(int, int);			//编辑TableWidget
	void changeCell(int, int);			//变更数据
private:
	bool Init(QString);					//根据数据库的路径,打开数据库,连接控件之间的槽函数
	//void bulidTable();	
	void getInfo();						//获取数据库中的信息,并动态添加控件

	Ui::EmbeddedSystemClass ui;
	SQLite sql;
	QMenu* m_Operations;
	QMenu* m_Tables;
	map a_tables;
	QAction* a_Insert;
	QAction* a_Delete;
	QAction* a_Search;
	QAction* a_Drop;
	QAction* a_Open;

	vector searchIndex;			//保存查找的索引
	//flags
	bool double_clicked = false;		//在TableWidget的cell上点两下
};

SQLite.cpp 

#include "EmbeddedSystem.h"

QStringList charType = { "TEXT","CHAR","VARCHAR","char","text","varchar" };

bool SQLite::openDatabase(const QString& bd)
{
	database = QSqlDatabase::addDatabase("QSQLITE");
	database.setDatabaseName(bd);
	if (!database.open())
	{
		return false;
	}
	query = new QSqlQuery(database);
	return true;
}

bool SQLite::bulidTables()
{
	//查询所有表的名称
	query->exec("SELECT name FROM sqlite_master WHERE type='table'");
	while (query->next())
	{
		Table* t = new Table();
		t->table_name = query->value(0).toString();
		Tables.push_back(t);
	}
	if (Tables.size() != 0)
	{
		cur_Index = 0;
	}
	//查询数据库表的列名以及数据类型
	for (int i = 0; i < Tables.size(); i++)
	{
		//查询表的列信息
		QString query_str = "pragma table_info ('" + Tables[i]->table_name + "')";
		if (!query->exec(query_str))
			return false;
		while (query->next())
		{
			Tables[i]->col_name << query->value(1).toString();
			Tables[i]->col_type << query->value(2).toString();
			//是否是主键
			if (query->value(5).toBool() == true)
			{
				Tables[i]->primaryKey = query->value(1).toString();
			}
		}
		//查询数据
		for (int j = 0; j < Tables[i]->col_name.size(); j++)
		{
			query_str = "select " + Tables[i]->col_name[j] + " from " + Tables[i]->table_name;
			if (!query->exec(query_str))
				return false;
			else
			{
				vector data;
				while (query->next())
				{
					data.push_back(query->value(0).toString());
				}
				if (Tables[i]->row_count == 0)
					Tables[i]->row_count = data.size();
				Tables[i]->data.push_back(data);
			}
		}
	}
	return true;
}

void SQLite::clearThis()
{
	database.close();
	for (int i = 0; i < Tables.size(); i++)
	{
		delete Tables[i];
	}
	Tables.clear();
}

const QStringList& SQLite::get_curColname()
{
	if (Tables.size() > 0)
		return Tables[cur_Index]->col_name;
	else
		return QStringList();
}

int SQLite::get_curColcount()
{
	if (Tables.size() > 0)
		return Tables[cur_Index]->col_name.size();
	else
		return 0;
}

int SQLite::get_curRowcount()
{
	if (Tables.size() > 0)
		return Tables[cur_Index]->row_count;
	else
		return 0;
}

QString SQLite::get_curTablename()
{
	if (Tables.size() > 0)
		return Tables[cur_Index]->table_name;
	else
		return QString();
}

const QStringList SQLite::get_Tablesname()
{
	QStringList tables_name;
	for (int i = 0; i < Tables.size(); i++)
		tables_name << Tables[i]->table_name;
	return tables_name;
}

const QStringList & SQLite::get_colTypes()
{
	return Tables[cur_Index]->col_type;
}

bool SQLite::newTble(const QStringList& table_info, const QStringList& type, int id)
{
	//create table
	QString query_str = "create table " + table_info[0] + " (";
	for (int i = 1; i < table_info.size(); i++)
	{
		if (id == i - 1)
			query_str += " " + table_info[i] + " " + type[i - 1] + " primary key " + ",";
		else
			query_str += " " + table_info[i] + " " + type[i - 1] + ",";
	}
	query_str = query_str.left(query_str.size() -1);
	query_str += ")";

	//new 对象
	if (!query->exec(query_str))
		return false;
	Table* new_table = new Table();
	new_table->table_name = table_info[0];
	new_table->col_type = type;
	for (int i = 1; i < table_info.size(); i++)
	{
		new_table->col_name << table_info[i];
		new_table->data.push_back(vector());
	}
	new_table->primaryKey = new_table->col_name[id];
	new_table->row_count = 0;
	Tables.push_back(new_table);
	return true;
}

const vector> &SQLite::get_cur_Tabledata()
{
	if (Tables.size() > 0)
		return Tables[cur_Index]->data;
	else
		return vector>();
}

const vector SQLite::get_row(int row)
{
	vector row_data;
	for (int i = 0; i < get_curColcount(); i++)
	{
		row_data.push_back(get_dataAt(row, i));
	}
	return row_data;
}

vector SQLite::get_Index(const QString& col_name, const QString& value)
{
	vector index;
	const QStringList& col_names = get_curColname();
	int i = 0;
	for (i; i < col_names.size(); i++)
		if (col_names[i] == col_name)
			break;
	for (int j = 0; j < Tables[cur_Index]->data[i].size(); j++)
		if (get_dataAt(j, i) == value)
		{
			index.push_back(j);
			if (col_name == get_primaryKey())
				break;
			continue;
		}
	return index;
}

QString SQLite::get_dataAt(int row, int col)
{
	return Tables[cur_Index]->data[col][row];
}

QString SQLite::get_primaryKey()
{
	return Tables[cur_Index]->primaryKey;
}

QString SQLite::get_colType(const QString& name)
{
	for (int i = 0; i < Tables[cur_Index]->col_name.size(); i++)
	{
		if (name == Tables[cur_Index]->col_name[i])
		{
			return Tables[cur_Index]->col_type[i];
		}
	}
	return QString();
}

void SQLite::changeTable(const QString& table_name)
{
	for (int i = 0; i < Tables.size(); i++)
	{
		if (Tables[i]->table_name == table_name)
		{
			if (cur_Index == i)
				return;
			else
			{
				cur_Index = i;
				emit need_update();
			}
		}
	}
}

const vector SQLite::searchValues(const QString&col_name, QString& value)
{
	vector searchIndex;
	/*
	if (!query->exec("select * from " + Tables[cur_Index]->table_name + " where " + col_name + " = " + toFormal(get_colType(col_name),value)))
		return searchIndex;
	else
	{*/
	int count = get_curColcount();
	const QStringList& col_names = get_curColname();
	searchIndex = get_Index(col_name, value);
	return searchIndex;
	//}
}

bool SQLite::deleteValues(const QString&col_name, QString& value)
{
	const QStringList& col_names = get_curColname();
	int col_num = -1;
	for (int i = 0; i < col_names.size(); i++)
	{
		if (col_names[i] == col_name)
			col_num = i;
	}
	//更改下格式
	QString f_value = toFormal(Tables[cur_Index]->col_type[col_num], value);
	if (!query->exec("delete from " + get_curTablename() + " where "+ col_name + " = " + f_value))
	{
		return false;
	}
	else
	{
		vector row;
		vector>& data = Tables[cur_Index]->data;

		for(int j = 0;jrow_count-=row.size();
		if (row.size() > 0)
			emit need_update();
		return true;
	}
}

bool SQLite::insertValues(const QString& table_name, QStringList& values)
{
	int index = searchTable(table_name);
	if (!query->exec("insert into " + table_name + " values (" + toFormal(Tables[index]->col_type, values) + ")"))
	{
		return false;
	}
	else
	{
		for (int i = 0; i < values.size(); i++)
		{
			Tables[index]->data[i].push_back(values[i]);
		}
		Tables[index]->row_count++;
		emit need_update();
		return true;
	}
}

int SQLite::searchTable(const QString& table_name)
{
	for (int i = 0; i < Tables.size(); i++)
	{
		if (Tables[i]->table_name == table_name)
			return i;
	}
	return -1;
}

bool SQLite::changeValues(const int row, const int col, QString& value)
{
	if (!query->exec("update " + get_curTablename() + " set " + get_curColname()[col]+ " = " + toFormal(Tables[cur_Index]->col_type[col],value)\
			+ " where " + get_primaryKey() + " = " + toFormal(Tables[cur_Index]->col_type[col],get_dataAt(row,col))))
	{
		return false;
	}
	else
	{
		Tables[cur_Index]->data[col][row] = value;
		return true;
	}
}

bool SQLite::removeTable(const QString& table_name)
{
	if (!query->exec("drop table " + table_name))
		return false;
	else
	{
		for (int i = 0; i < Tables.size(); i++)
		{
			if (Tables[i]->table_name == table_name)
			{
				Tables.erase(Tables.begin() + i);
				if (cur_Index == i)
					cur_Index = 0;
				return true;
			}
		}
	}
}

QString SQLite::toFormal(const QStringList& col_type, QStringList& values)
{
	QRegExp re;
	re.setPattern("[A-Za-z]+");
	QStringList return_values;
	for (int i = 0; i < col_type.size(); i++)
	{
		re.indexIn(col_type[i]);
		QString type = re.capturedTexts()[0];
		if (charType.contains(type))
		{
			return_values << "'" + values[i] + "'";
		}
		else
		{
			return_values << values[i];
		}
	}
	return return_values.join(',');
}

QString SQLite::toFormal(const QString& col_type, QString& value)
{
	QRegExp re;
	QString f_value = value;
	re.setPattern("[A-Za-z]+");
	re.indexIn(col_type);
	QString type = re.capturedTexts()[0];
	if (charType.contains(type))
	{
		f_value = "'" + value + "'";
	}
	return f_value;
}

SQLite::SQLite()
{
	
}

SQLite::~SQLite()
{
	database.close();
	delete query;
	for (int i = 0; i < Tables.size(); i++)
	{
		delete Tables[i];
	}
}

QString get_errorMsg(SQLite& sql)
{
	return sql.query->lastError().databaseText();
}

 EmbeddedSystem.cpp

#include "EmbeddedSystem.h"
#define DEFAULT_COL 3

QString DEFAULT_DB = "project.db";

EmbeddedSystem::EmbeddedSystem(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	if (Init(DEFAULT_DB))
	{
		getInfo();
		Update();
	}
}

bool EmbeddedSystem::Init(QString db)
{
	if (!sql.openDatabase(db))
	{
		QMessageBox(QMessageBox::Icon::Critical, "错误", "打开数据库失败").exec();
		return false;
	}
	
	this->setWindowTitle(db);

	m_Tables = new QMenu("Tables",ui.menuBar);
	m_Operations = new QMenu("Operations",ui.menuBar);
	ui.menuBar->addMenu(m_Tables);
	ui.menuBar->addMenu(m_Operations);

	a_Insert = new QAction("Insert",m_Operations);
	a_Delete = new QAction("Delete",m_Operations);
	a_Search = new QAction("Search",m_Operations);
	a_Drop = new QAction("Drop", m_Operations);
	a_Open = new QAction("Open", m_Operations);

	//Menu添加Action
	m_Operations->addAction(a_Insert);
	m_Operations->addAction(a_Delete);
	m_Operations->addAction(a_Search);
	m_Operations->addAction(a_Drop);
	m_Operations->addAction(a_Open);

	//Action 各自关联对应的函数
	connect(&sql, SIGNAL(need_update()), this, SLOT(Update()));
	connect(a_Insert, SIGNAL(triggered()), this, SLOT(Insert()));
	connect(a_Delete, SIGNAL(triggered()), this, SLOT(Delete()));
	connect(a_Search, SIGNAL(triggered()), this, SLOT(Search()));
	connect(a_Drop, SIGNAL(triggered()), this, SLOT(Drop()));
	connect(a_Open, SIGNAL(triggered()), this, SLOT(Open()));
	//TableWidget的Signal关联函数
	connect(ui.tableWidget, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(editCell(int, int)));
	connect(ui.tableWidget, SIGNAL(cellChanged(int, int)), this, SLOT(changeCell(int, int)));
	//Table切换关联函数
	connect(m_Tables, SIGNAL(triggered(QAction*)), this, SLOT(On_changeTables(QAction*)));
	return true;
}

void EmbeddedSystem::getInfo()
{
	if (!sql.bulidTables())
	{
		QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
		this->close();
	}
	QStringList tables_name = sql.get_Tablesname();
	for (int i = 0; i < tables_name.size(); i++)
	{
		QAction* action = new QAction(tables_name[i], m_Tables);
		a_tables[tables_name[i]] = action;

		m_Tables->addAction(action);
	}
	QAction* new_table = new QAction("new Table", m_Tables);
	m_Tables->addAction(new_table);

	if(tables_name.size()>0)
		m_Tables->setTitle("Table : " + sql.get_curTablename());
}

void EmbeddedSystem::Update()
{
	ui.tableWidget->clear();
	ui.tableWidget->setRowCount(sql.get_curRowcount());
	ui.tableWidget->setColumnCount(sql.get_curColcount());

	ui.tableWidget->setHorizontalHeaderLabels(sql.get_curColname());
	if (searchIndex.size() == 0)
	{
		const vector> data = sql.get_cur_Tabledata();
		if (data.size() == 0) return;

		for (int i = 0; i < data[0].size(); i++)
		{
			for (int j = 0; j < data.size(); j++)
			{
				QTableWidgetItem* item = new QTableWidgetItem(data[j][i]);
				ui.tableWidget->setItem(i, j, item);
			}
		}
	}
	else
	{
		vector> data;
		for (int i = 0; i < searchIndex.size(); i++)
		{
			data.push_back(sql.get_row(searchIndex[i]));
		}
		for (int i = 0; i < data.size(); i++)
		{
			for (int j = 0; j < data[0].size(); j++)
			{
				QTableWidgetItem* item = new QTableWidgetItem(data[i][j]);
				ui.tableWidget->setItem(i, j, item);
			}
		}
	}
	this->update();
}

void EmbeddedSystem::Insert()
{
	const QStringList& col_name = sql.get_curColname();
	const QStringList& col_type = sql.get_colTypes();
	QDialog dialog;
	dialog.setWindowTitle("Insert");
	QGridLayout *layout = new QGridLayout(&dialog);
	QPushButton *ok = new QPushButton("OK",&dialog);
	QStringList recvData;
	for (int i = 0; i < col_name.size(); i++)
	{
		//添加列名信息
		layout->addWidget(new QLabel(col_name[i] + " " + col_type[i],&dialog), 0, i);
		//添加LineEdit供编辑
		layout->addWidget(new QLineEdit(&dialog), 1, i);
	}
	layout->addWidget(ok, 1, col_name.size());
	auto ok_clicked = [&]() {
		for (int i = 0; i < col_name.size(); i++)
		{
			//获取LineEdit的编辑信息
			QLayoutItem* it = layout->itemAtPosition(1, i);
			QLineEdit* lineEdit = qobject_cast(it->widget());
			recvData << lineEdit->text();
		}
		//调用sql对象插入值
		if (!sql.insertValues(sql.get_curTablename(), recvData))
		{
			QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
		}
		dialog.close();
	};
	//关联lambda
	connect(ok, &QPushButton::clicked, this, ok_clicked);
	dialog.setLayout(layout);
	dialog.exec();
}

void EmbeddedSystem::Delete()
{
	const QStringList& col_name = sql.get_curColname();
	QDialog dialog;
	dialog.setWindowTitle("Delete");
	QGridLayout* layout = new QGridLayout(&dialog);
	QPushButton* ok = new QPushButton("OK", &dialog);
	//用于实现Menu Button
	QPushButton* choice = new QPushButton(col_name[0],&dialog);
	//避免text导致的控件缩放
	choice->setFixedWidth(100);
	QMenu* menu = new QMenu(col_name[0],&dialog);
	QLineEdit* lineEdit = new QLineEdit(&dialog);

	//将鼠标点中的Action上的文字显示在Button上
	auto action_triggered = [&](QAction* action) {
		choice->setText(action->text());
	};
	//同Insert类似
	auto ok_clicked = [&]() {
		QString cur_name = choice->text();
		QString delete_data = lineEdit->text();
		if (!sql.deleteValues(cur_name, delete_data))
		{
			QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
		}
		dialog.close();
	};
	for (int i = 0; i < col_name.size(); i++)
	{
		menu->addAction(new QAction(col_name[i], menu));
	}
	choice->setMenu(menu);
	connect(menu, &QMenu::triggered, this, action_triggered);
	connect(ok, &QPushButton::clicked, this, ok_clicked);
	layout->addWidget(choice, 0, 0);
	layout->addWidget(lineEdit, 0, 1);
	layout->addWidget(ok, 0, 2);
	dialog.setLayout(layout);
	dialog.exec();
}

void EmbeddedSystem::Search()
{
	const QStringList& col_name = sql.get_curColname();
	QDialog dialog;
	dialog.setWindowTitle("Search");
	QGridLayout* layout = new QGridLayout(&dialog);
	QPushButton* ok = new QPushButton("OK", &dialog);
	QPushButton* choice = new QPushButton(col_name[0], &dialog);
	choice->setFixedWidth(100);
	QMenu* menu = new QMenu(col_name[0], &dialog);
	QLineEdit* lineEdit = new QLineEdit(&dialog);

	auto action_triggered = [&](QAction* action) {
		choice->setText(action->text());
	};
	auto ok_clicked = [&]() {
		QString cur_name = choice->text();
		QString delete_data = lineEdit->text();
		if (delete_data.size() != 0)
			searchIndex = sql.searchValues(cur_name, delete_data);
		else
			searchIndex.clear();
		Update();
		dialog.close();
	};
	for (int i = 0; i < col_name.size(); i++)
	{
		menu->addAction(new QAction(col_name[i], menu));
	}
	choice->setMenu(menu);
	connect(menu, &QMenu::triggered, this, action_triggered);
	connect(ok, &QPushButton::clicked, this, ok_clicked);
	layout->addWidget(choice, 0, 0);
	layout->addWidget(lineEdit, 0, 1);
	layout->addWidget(ok, 0, 2);
	dialog.setLayout(layout);
	dialog.exec();
}

void EmbeddedSystem::Drop()
{
	const QStringList& col_name = sql.get_curColname();
	QDialog dialog;
	dialog.setWindowTitle("Drop");
	QGridLayout* layout = new QGridLayout(&dialog);
	QPushButton* ok = new QPushButton("OK", &dialog);

	layout->addWidget(new QLabel("Table Name:", &dialog),0,0);
	layout->addWidget(new QLineEdit(&dialog),0,1);
	layout->addWidget(ok, 0, 2);

	auto ok_clicked = [&]() {
		//获取table名字
		QString table_name = qobject_cast(layout->itemAtPosition(0, 1)->widget())->text();
		//删除table
		if (!sql.removeTable(table_name))
		{
			QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
		}
		else
		{
			//成功删除,则继续
			m_Tables->removeAction(a_tables[table_name]);
			a_tables.erase(table_name);

			if (m_Tables->title() == "Table : " + table_name)
				m_Tables->setTitle("Table : " + sql.get_curTablename());
			Update();
		}
		dialog.close();
	};

	connect(ok, &QPushButton::clicked, this, ok_clicked);
	dialog.setLayout(layout);
	dialog.exec();
}

void EmbeddedSystem::Open()
{
	QString path = QFileDialog().getOpenFileName(NULL, "Open Database", "", "*.db");
	if (path.size() != 0)
	{
		sql.clearThis();
		ui.tableWidget->clear();
		ui.menuBar->clear();
		Init(path);
		getInfo();
		Update();
	}
}

void EmbeddedSystem::changeCell(int row, int col)
{
	if (double_clicked)	//为了防止第一次写入数据的时候触发
	{
		double_clicked = false;
		QTableWidgetItem* item = ui.tableWidget->item(row, col);
		if (searchIndex.size() != 0)
		{
			if (!sql.changeValues(searchIndex[row], col, item->text()))
			{
				QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
			}
		}
		else
		{
			if (!sql.changeValues(row, col, item->text()))
			{
				QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
			}
		}
		return;
	}
}

void EmbeddedSystem::editCell(int, int)
{
	double_clicked = true;	
}

void EmbeddedSystem::On_changeTables(QAction* a)
{
	//如果Action的文字是新建table则新建
	if (a->text() == QString("new Table"))
	{
		QDialog dialog;
		dialog.setWindowTitle("new Table");
		QGridLayout* layout = new QGridLayout(&dialog);
		QPushButton* ok = new QPushButton("OK", &dialog);
		QPushButton* new_col = new QPushButton("new", &dialog);			//用来新加一列
		QStringList recvData;
		QButtonGroup* check_Group = new QButtonGroup(&dialog);			//设置主键只有一个
		layout->addWidget(new QLabel("Table Name", &dialog), 0, 0);		
		layout->addWidget(new QLineEdit(&dialog), 0, 1);				//用来编辑table的名字
		
		layout->addWidget(new QLabel("Col Name",&dialog), 1, 0);
		layout->addWidget(new QLabel("Col Type",&dialog), 1, 1);
		layout->addWidget(new QLabel("Primary Key", &dialog), 1, 2);

		int i = 2;
		int primary_key_index[] = { -1,-1 };

		for (i; i < DEFAULT_COL+1; i++)								
		{	
			layout->addWidget(new QLineEdit(&dialog), i, 0);			//默认存在两行,列名
			layout->addWidget(new QLineEdit(&dialog), i, 1);			//列数据类型
			QCheckBox* check_box = new QCheckBox(&dialog);				//用来选择是否是主键
			layout->addWidget(check_box, i, 2);
			check_Group->addButton(check_box);
			check_Group->setId(check_box, i - 2);
		}
		layout->addWidget(new_col, DEFAULT_COL + 1, 0);
		layout->addWidget(ok, DEFAULT_COL+1, 2);
		auto ok_clicked = [&]() {
			QStringList table_info;
			QStringList type;
			int Primary_keyId;
			//得到表名
			table_info << qobject_cast(layout->itemAtPosition(0, 1)->widget())->text();

			for (int row = 2; row < i; row++)
			{
				//得到列名
				table_info << qobject_cast(layout->itemAtPosition(row, 0)->widget())->text();
				//得到列数据类型
				type<< qobject_cast(layout->itemAtPosition(row, 1)->widget())->text();
			}
			//得到主键id
			Primary_keyId = check_Group->checkedId();
			if (!sql.newTble(table_info,type,Primary_keyId))
			{
				QMessageBox(QMessageBox::Icon::Critical, "错误", get_errorMsg(sql)).exec();
			}
			else
			{
				a_tables[table_info[0]] = a;
				a->setText(table_info[0]);

				m_Tables->addAction("new Table");
			}
			dialog.close();
			Update();
		};
		auto new_col_clicked = [&]() {
			//点一下就加一列
			layout->removeWidget(ok);
			layout->removeWidget(new_col);

			layout->addWidget(new QLineEdit(&dialog), i, 0);
			layout->addWidget(new QLineEdit(&dialog), i, 1);
			QCheckBox* check_box = new QCheckBox(&dialog);
			layout->addWidget(check_box, i, 2);
			check_Group->addButton(check_box);
			check_Group->setId(check_box, i - 2);
			i++;
			layout->addWidget(new_col,i, 0);
			layout->addWidget(ok, i, 2);
		};
		
		connect(new_col, &QPushButton::clicked, this, new_col_clicked);
		connect(ok, &QPushButton::clicked, this, ok_clicked);
		dialog.setLayout(layout);
		dialog.exec();
	}
	else
	{
		//选择表
		m_Tables->setTitle("Table : " + a->text());	
		sql.changeTable(a->text());
	}
}

EmbeddedSystem::~EmbeddedSystem()
{

}

 

你可能感兴趣的:(Qt,可视化显示)