123 lines
3.2 KiB
C++
123 lines
3.2 KiB
C++
#include "User.h"
|
|
#include <QLabel>
|
|
#include <QDebug>
|
|
#include "Global.h"
|
|
User::User(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
ui.setupUi(this);
|
|
|
|
this->setWindowTitle("账户管理");
|
|
// 设置窗口图标
|
|
this->setWindowIcon(QIcon(":/images/Images/client.ico"));
|
|
setWindowFlags(Qt::Dialog);
|
|
setWindowFlags(Qt::WindowCloseButtonHint);
|
|
this->setFixedSize(400, 500);
|
|
|
|
// 读取配置
|
|
m_users = g_config->usersList();
|
|
for (int i = 0; i < m_users.count(); i++)
|
|
{
|
|
QMap<QString, QString> user = m_users[i];
|
|
// 添加视图
|
|
int index = ui.tableWidget->rowCount();
|
|
ui.tableWidget->insertRow(index);
|
|
ui.tableWidget->setItem(index, 0, new QTableWidgetItem(user["username"]));
|
|
ui.tableWidget->setItem(index, 1, new QTableWidgetItem(user["password"]));
|
|
}
|
|
|
|
// 设置禁止编辑
|
|
ui.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
// 设置选择行为为整行选中
|
|
ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
// 设置表头拉伸方式
|
|
ui.tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
|
// 设置自定义上下文菜单方案
|
|
ui.tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
// 创建自定义菜单
|
|
m_menu = new QMenu(ui.tableWidget);
|
|
m_actionDelete = new QAction("删除", this);
|
|
m_menu->addAction(m_actionDelete);
|
|
// 连接自定义菜单请求与槽
|
|
connect(ui.tableWidget, &QTableWidget::customContextMenuRequested, this, [=](QPoint point) {
|
|
m_menu->exec(QCursor::pos());
|
|
});
|
|
// 连接动作触发请求与槽
|
|
connect(m_actionDelete, &QAction::triggered, this,&User::slotDelete);
|
|
// 设置双击事件
|
|
connect(ui.tableWidget, &QTableWidget::itemDoubleClicked, this, [=]() {
|
|
int row = ui.tableWidget->currentRow();
|
|
QString username = ui.tableWidget->item(row, 0)->text();
|
|
QString password = ui.tableWidget->item(row, 1)->text();
|
|
emit signalSetUser(username, password);
|
|
});
|
|
|
|
}
|
|
|
|
User::~User()
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::slotDelete(bool ok)
|
|
{
|
|
int row = ui.tableWidget->currentRow();
|
|
QString username = ui.tableWidget->item(row, 0)->text();
|
|
QString password = ui.tableWidget->item(row, 1)->text();
|
|
// 删除视图
|
|
ui.tableWidget->removeRow(row);
|
|
|
|
// 删除配置
|
|
for (int i = 0; i < m_users.count();i++)
|
|
{
|
|
QMap<QString, QString> user = m_users[i];
|
|
if (user["username"] == username && user["password"] == password)
|
|
{
|
|
m_users.removeAt(i);
|
|
break;
|
|
}
|
|
}
|
|
g_config->setUsersList(m_users);
|
|
}
|
|
|
|
|
|
void User::slotAddUser(QString username, QString password)
|
|
{
|
|
bool found = false;
|
|
QList<QMap<QString, QString>>::iterator iter;
|
|
for (iter = m_users.begin(); iter != m_users.end(); iter++)
|
|
{
|
|
QMap<QString, QString> user = *iter;
|
|
if (user["username"] == username && user["password"] == password)
|
|
{
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
// 避免重复添加
|
|
if (found)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (username.isEmpty() || password.isEmpty())
|
|
{
|
|
return;
|
|
}
|
|
// 添加视图
|
|
int index = ui.tableWidget->rowCount();
|
|
ui.tableWidget->insertRow(index);
|
|
ui.tableWidget->setItem(index, 0, new QTableWidgetItem(username));
|
|
ui.tableWidget->setItem(index, 1, new QTableWidgetItem(password));
|
|
|
|
// 保存配置
|
|
QMap<QString, QString> user;
|
|
user["username"] = username;
|
|
user["password"] = password;
|
|
m_users.append(user);
|
|
g_config->setUsersList(m_users);
|
|
} |