#include "Phonebook.h"
#include "ui_Phonebook.h"
Phonebook::Phonebook(QWidget *parent)
: QWidget(parent)
{
ui = new Ui::Phonebook();
ui->setupUi(this);
disp();
}
Phonebook::~Phonebook()
{
qDebug() << "destory";
delete ui;
}
void Phonebook::on_Btn_logout_clicked(bool) {
emit sendlogoutsignals(true);
this->close();
}
void Phonebook::disp() {
model = new QSqlTableModel(this);
model->setTable("phonebook");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setFilter(QObject::tr("susername = '%1'").arg(this->username));
model->select(); //选取整个表的所有行
//model->removeColumn(0);
model->setHeaderData(2, Qt::Orientation::Horizontal, QStringLiteral("姓名"));
model->setHeaderData(3, Qt::Orientation::Horizontal, QStringLiteral("电话"));
model->setHeaderData(4, Qt::Orientation::Horizontal, QStringLiteral("E-mail"));
ui->tableView->setModel(model);
ui->tableView->hideColumn(0);
ui->tableView->hideColumn(1);
}
void Phonebook::setUserName(QString name)
{
this->username = name;
}
void Phonebook::fresh()
{
disp();
}
void Phonebook::on_Btn_new_clicked(bool)
{
Addcontact* AC = new Addcontact();
AC->setUserName(this->username);
connect(AC, SIGNAL(addnewconfsignals()), this, SLOT(disp()));
AC->show();
}
void Phonebook::on_Btn_save_clicked(bool)
{
if (model == NULL)return;
qDebug() << "submit:" << model->submitAll();
}
void Phonebook::on_Btn_undo_clicked(bool) {
if (model == NULL)return;
model->revertAll();
}
void Phonebook::on_Btn_delete_clicked(bool) {
int curRow = ui->tableView->currentIndex().row();
model->removeRow(curRow);
int ok = QMessageBox::warning(this, QStringLiteral("删除联系人!"), QStringLiteral("你确定删除当前联系人吗?\n删除后将不能撤销"),
QMessageBox::Yes, QMessageBox::No);
if (ok == QMessageBox::No)
{
model->revertAll(); //如果不删除,则撤销
}
else model->submitAll(); //否则提交,在数据库中删除该行
}
void Phonebook::on_Btn_all_clicked(bool)
{
disp();
}
void Phonebook::on_Btn_Qname_clicked(bool)
{
QString name = ui->LE_Qname->text();
if (model == NULL)return;
model->setFilter(QObject::tr("susername = '%1' and sname = '%2'").arg(this->username).arg(name)); //根据姓名进行筛选
model->select();
model->setHeaderData(2, Qt::Orientation::Horizontal, QStringLiteral("姓名"));
model->setHeaderData(3, Qt::Orientation::Horizontal, QStringLiteral("电话"));
model->setHeaderData(4, Qt::Orientation::Horizontal, QStringLiteral("E-mail"));
ui->tableView->setModel(model);
ui->tableView->hideColumn(0);
ui->tableView->hideColumn(1);
}
void Phonebook::on_Btn_Qphone_clicked(bool)
{
QString phone = ui->LE_Qphone->text();
if (model == NULL)return;
model->setFilter(QObject::tr("susername = '%1' and snumber = '%2'").arg(this->username).arg(phone)); //根据姓名进行筛选
model->select();
model->setHeaderData(2, Qt::Orientation::Horizontal, QStringLiteral("姓名"));
model->setHeaderData(3, Qt::Orientation::Horizontal, QStringLiteral("电话"));
model->setHeaderData(4, Qt::Orientation::Horizontal, QStringLiteral("E-mail"));
ui->tableView->setModel(model);
ui->tableView->hideColumn(0);
ui->tableView->hideColumn(1);
}
void Phonebook::on_Btn_import_clicked(bool)
{
QFileDialog* fileDialog = new QFileDialog(this);
fileDialog->setWindowTitle(tr("Open"));//设置文件保存对话框的标题
fileDialog->setAcceptMode(QFileDialog::AcceptOpen);//设置文件对话框为保存模式
fileDialog->setFileMode(QFileDialog::AnyFile);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件
fileDialog->setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件名,大小,创建日期等信息;
fileDialog->setDirectory(".");
fileDialog->setNameFilter(tr("csv (*.csv)"));
if (fileDialog->exec() == QFileDialog::Accepted) {
QString filename = fileDialog->selectedFiles()[0];
QSqlQuery query;
if (!query.exec(QObject::tr("load data infile '%1'\
into table phonebook\
fields terminated by ',' optionally enclosed by '\"' escaped by '\"' \
lines terminated by '\r\n'\
(@sid,susername,sname, snumber,semail) set susername = '%2';").arg(filename).arg(this->username))) {
QMessageBox::warning(this, QStringLiteral("导入错误!"), QString(query.lastError().text()), QMessageBox::Yes);
}
else {
disp();
QMessageBox::information(this, QStringLiteral("消息"), QStringLiteral("导入成功!"), QMessageBox::Yes, QMessageBox::Yes);
}
}
disp();
}
void Phonebook::on_Btn_export_clicked(bool)
{
QFileDialog* fileDialog = new QFileDialog(this);
fileDialog->setWindowTitle(tr("Save As"));//设置文件保存对话框的标题
fileDialog->setAcceptMode(QFileDialog::AcceptSave);//设置文件对话框为保存模式
fileDialog->setFileMode(QFileDialog::AnyFile);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件
fileDialog->setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件名,大小,创建日期等信息;
fileDialog->setDirectory(".");
fileDialog->setNameFilter(tr("csv (*.csv)"));
if (fileDialog->exec() == QFileDialog::Accepted) {
QString filename = fileDialog->selectedFiles()[0];
QSqlQuery query;
if (!query.exec(QObject::tr("select * from phonebook where susername = '%1'\
into outfile '%2'\
fields terminated by ',' optionally enclosed by '\"'\
lines terminated by '\r\n'; ").arg(this->username).arg(filename))) {
QMessageBox::warning(this, QStringLiteral("导出错误!"), QString(query.lastError().text()), QMessageBox::Yes);
}
else {
QMessageBox::information(this, QStringLiteral("消息"), QStringLiteral("导出成功!"), QMessageBox::Yes, QMessageBox::Yes);
}
}
}
void Phonebook::on_Btn_merge_clicked(bool)
{
QSqlQuery query;
qDebug() << query.exec(QObject::tr("CREATE TABLE IF NOT EXISTS merge (\
sid INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,\
susername varchar(40),\
sname varchar(255) default NULL,\
snumber varchar(255) default NULL,\
semail varchar(255) default NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8;"));
QFileDialog* fileDialog = new QFileDialog(this);
fileDialog->setWindowTitle(tr("Select"));//设置文件保存对话框的标题
fileDialog->setAcceptMode(QFileDialog::AcceptOpen);//设置文件对话框为保存模式
fileDialog->setFileMode(QFileDialog::ExistingFiles);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件
fileDialog->setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件名,大小,创建日期等信息;
fileDialog->setDirectory(".");
fileDialog->setNameFilter(tr("csv (*.csv)"));
if (fileDialog->exec() == QFileDialog::Accepted) {
QStringList filenames = fileDialog->selectedFiles();
for (int i = 0; i < filenames.size(); ++i) {
qDebug() << filenames[i];
if (!query.exec(QObject::tr("load data infile '%1'\
into table merge\
fields terminated by ',' optionally enclosed by '\"' escaped by '\"' \
lines terminated by '\r\n'\
(@sid,susername,sname, snumber,semail) set susername = '%2';").arg(filenames[i]).arg(QString("merge")))) {
QMessageBox::warning(this, QStringLiteral("合并错误!"), QString(query.lastError().text()), QMessageBox::Yes);
}
}
}
fileDialog->setWindowTitle(tr("Save As"));//设置文件保存对话框的标题
fileDialog->setAcceptMode(QFileDialog::AcceptSave);//设置文件对话框为保存模式
fileDialog->setFileMode(QFileDialog::AnyFile);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件
fileDialog->setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件名,大小,创建日期等信息;
fileDialog->s
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源内项目代码都是经过测试运行成功,功能正常的情况下才上传的,请放心下载使用。 2、适用人群:主要针对计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、数学、电子信息等)的同学或企业员工下载使用,具有较高的学习借鉴价值。 3、不仅适合小白学习实战练习,也可作为大作业、课程设计、毕设项目、初期项目立项演示等,欢迎下载,互相学习,共同进步!
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 30 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
龙年行大运
- 粉丝: 1008
- 资源: 3848
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)