121 lines
3.9 KiB
C++
121 lines
3.9 KiB
C++
#include "countrymanagement.h"
|
|
#include "dbunit.h"
|
|
#include <QDebug>
|
|
#include <QGridLayout>
|
|
#include <QInputDialog>
|
|
#include <QPushButton>
|
|
#include <QSqlError>
|
|
#include <QSqlQuery>
|
|
|
|
using namespace ViewComp;
|
|
|
|
CountryManagement::CountryManagement(DBUnit* database, QWidget* parent)
|
|
: QDialog(parent), db_host(database), keywords_input(new QLineEdit(this)),
|
|
country_view(new QTableView(this)), country_model(new QStandardItemModel(this)),
|
|
append(new QPushButton("添加", this)), remove(new QPushButton("移除", this))
|
|
{
|
|
country_view->setModel(country_model);
|
|
|
|
// ============================
|
|
auto layout = new QGridLayout(this);
|
|
layout->addWidget(keywords_input, 0, 0, 1, 4);
|
|
layout->addWidget(country_view, 1, 0, 3, 4);
|
|
layout->addWidget(append, 4, 0, 1, 2);
|
|
layout->addWidget(remove, 4, 2, 1, 2);
|
|
country_model->setHorizontalHeaderLabels(QStringList() << "国家名称"
|
|
<< "简介");
|
|
|
|
connect(append, &QPushButton::clicked, [this]() {
|
|
QInputDialog in(this);
|
|
in.setWindowTitle("输入国家名称");
|
|
in.setLabelText("输入非重复国家名称:");
|
|
if (in.exec() == QDialog::Rejected)
|
|
return;
|
|
|
|
auto q = db_host->getQuery();
|
|
q.prepare("insert into country_information "
|
|
"(name, describe)values(:n, :d)");
|
|
q.bindValue(":n", in.textValue());
|
|
q.bindValue(":d", "请手动修改国家简述");
|
|
if (!q.exec()) {
|
|
qDebug() << q.lastError();
|
|
return;
|
|
}
|
|
q.prepare("select max(cid) from country_information");
|
|
if (!q.exec()) {
|
|
qDebug() << q.lastError();
|
|
return;
|
|
}
|
|
if (q.next()) {
|
|
QList<QStandardItem*> row;
|
|
row << new QStandardItem(in.textValue());
|
|
row.last()->setData(q.value(0));
|
|
row << new QStandardItem("请手动修改国家简述");
|
|
country_model->appendRow(row);
|
|
}
|
|
});
|
|
connect(remove, &QPushButton::clicked, [this]() {
|
|
auto index = country_view->currentIndex();
|
|
if (!index.isValid())
|
|
return;
|
|
|
|
auto inindex = index.sibling(index.row(), 0);
|
|
auto q = db_host->getQuery();
|
|
q.prepare("delete from country_information where cid = :id");
|
|
q.bindValue(":id", inindex.data(Qt::UserRole + 1));
|
|
if (!q.exec()) {
|
|
qDebug() << q.lastError();
|
|
return;
|
|
}
|
|
|
|
country_model->removeRow(index.row());
|
|
});
|
|
|
|
connect(keywords_input, &QLineEdit::textChanged, this, &CountryManagement::input_query);
|
|
}
|
|
|
|
void
|
|
CountryManagement::init_country_model(QStandardItemModel* model)
|
|
{
|
|
model->removeRows(0, model->rowCount());
|
|
auto q = db_host->getQuery();
|
|
q.prepare("select cid, name, describe from country_information");
|
|
if (!q.exec()) {
|
|
qDebug() << q.lastError();
|
|
return;
|
|
}
|
|
while (q.next()) {
|
|
QList<QStandardItem*> row;
|
|
row << new QStandardItem(q.value(1).toString());
|
|
row.last()->setData(q.value(0));
|
|
row << new QStandardItem(q.value(2).toString());
|
|
country_model->appendRow(row);
|
|
}
|
|
}
|
|
|
|
void
|
|
CountryManagement::input_query(const QString& keywords)
|
|
{
|
|
country_model->removeRows(0, country_model->rowCount());
|
|
if (keywords.isEmpty())
|
|
return;
|
|
|
|
QString str = "select cid, name, describe from country_information ";
|
|
if (keywords != "*") {
|
|
str += "where name like '%" + keywords + "%'";
|
|
}
|
|
auto q = db_host->getQuery();
|
|
q.prepare(str);
|
|
if (!q.exec()) {
|
|
qDebug() << q.lastError();
|
|
return;
|
|
}
|
|
while (q.next()) {
|
|
QList<QStandardItem*> row;
|
|
row << new QStandardItem(q.value(1).toString());
|
|
row.last()->setData(q.value(0));
|
|
row << new QStandardItem(q.value(2).toString());
|
|
country_model->appendRow(row);
|
|
}
|
|
}
|