新增国家编辑器,修复标签编辑器
This commit is contained in:
parent
6da7cf4d48
commit
b730b066cf
|
@ -23,13 +23,17 @@ msvc {
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
corehost.cpp \
|
corehost.cpp \
|
||||||
|
countrymanagement.cpp \
|
||||||
dbunit.cpp \
|
dbunit.cpp \
|
||||||
|
labelsmanagement.cpp \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
mainwindow.cpp
|
mainwindow.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
corehost.h \
|
corehost.h \
|
||||||
|
countrymanagement.h \
|
||||||
dbunit.h \
|
dbunit.h \
|
||||||
|
labelsmanagement.h \
|
||||||
mainwindow.h
|
mainwindow.h
|
||||||
|
|
||||||
# Default rules for deployment.
|
# Default rules for deployment.
|
||||||
|
|
|
@ -0,0 +1,120 @@
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef COUNTRYMANAGEMENT_H
|
||||||
|
#define COUNTRYMANAGEMENT_H
|
||||||
|
|
||||||
|
#include <QDialog>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QStandardItemModel>
|
||||||
|
#include <QTableView>
|
||||||
|
|
||||||
|
class DBUnit;
|
||||||
|
|
||||||
|
namespace ViewComp {
|
||||||
|
class CountryManagement : public QDialog {
|
||||||
|
public:
|
||||||
|
CountryManagement(DBUnit* database, QWidget* parent = nullptr);
|
||||||
|
|
||||||
|
private:
|
||||||
|
DBUnit* const db_host;
|
||||||
|
|
||||||
|
QLineEdit* const keywords_input;
|
||||||
|
QTableView* const country_view;
|
||||||
|
QStandardItemModel* const country_model;
|
||||||
|
QPushButton *const append, *const remove;
|
||||||
|
|
||||||
|
void init_country_model(QStandardItemModel* model);
|
||||||
|
void input_query(const QString& keywords);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // COUNTRYMANAGEMENT_H
|
BIN
example.db
BIN
example.db
Binary file not shown.
|
@ -8,6 +8,8 @@
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
|
|
||||||
|
using namespace ViewComp;
|
||||||
|
|
||||||
LabelsManagement::LabelsManagement(DBUnit* tool, QWidget* parent)
|
LabelsManagement::LabelsManagement(DBUnit* tool, QWidget* parent)
|
||||||
: QDialog(parent), db_host(tool), keywords_input(new QLineEdit(this)),
|
: QDialog(parent), db_host(tool), keywords_input(new QLineEdit(this)),
|
||||||
labels_view(new QTableView(this)), labels_model(new QStandardItemModel(this)),
|
labels_view(new QTableView(this)), labels_model(new QStandardItemModel(this)),
|
||||||
|
@ -46,11 +48,13 @@ LabelsManagement::LabelsManagement(DBUnit* tool, QWidget* parent)
|
||||||
qDebug() << q.lastError();
|
qDebug() << q.lastError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QList<QStandardItem*> row;
|
if (q.next()) {
|
||||||
row << new QStandardItem(in.textValue());
|
QList<QStandardItem*> row;
|
||||||
row.last()->setData(q.value(0), Qt::UserRole + 1);
|
row << new QStandardItem(in.textValue());
|
||||||
row << new QStandardItem("请手动修改标签简介");
|
row.last()->setData(q.value(0), Qt::UserRole + 1);
|
||||||
labels_model->appendRow(row);
|
row << new QStandardItem("请手动修改标签简介");
|
||||||
|
labels_model->appendRow(row);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
connect(remove, &QPushButton::clicked, [this]() {
|
connect(remove, &QPushButton::clicked, [this]() {
|
||||||
auto index = labels_view->currentIndex();
|
auto index = labels_view->currentIndex();
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
class DBUnit;
|
class DBUnit;
|
||||||
|
|
||||||
|
namespace ViewComp {
|
||||||
class LabelsManagement : public QDialog {
|
class LabelsManagement : public QDialog {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
@ -27,4 +28,6 @@ private:
|
||||||
void input_query(const QString& keywords);
|
void input_query(const QString& keywords);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#endif // LABELSMANAGEMENT_H
|
#endif // LABELSMANAGEMENT_H
|
||||||
|
|
3
main.cpp
3
main.cpp
|
@ -1,3 +1,4 @@
|
||||||
|
#include "countrymanagement.h"
|
||||||
#include "dbunit.h"
|
#include "dbunit.h"
|
||||||
#include "labelsmanagement.h"
|
#include "labelsmanagement.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
|
@ -10,7 +11,7 @@ int main(int argc, char *argv[])
|
||||||
// w.show();
|
// w.show();
|
||||||
|
|
||||||
DBUnit tool;
|
DBUnit tool;
|
||||||
LabelsManagement m(&tool);
|
ViewComp::CountryManagement m(&tool);
|
||||||
m.show();
|
m.show();
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
|
|
Loading…
Reference in New Issue