#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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:数据集、包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
基于QT和mysql的电话本管理系统.zip (30个子文件)
cm
.gitattributes 2KB
Mytelephonebook.sln 1KB
Mytelephonebook
Login.ui 4KB
contactdb.h 270B
Login.h 427B
Addcontact.cpp 858B
Usermanage.h 596B
contactdb.cpp 2KB
Signup.cpp 1KB
Mytelephonebook.vcxproj.filters 3KB
Usermanage.cpp 2KB
main.cpp 322B
Phonebook.ui 5KB
GeneratedFiles
ui_Addcontact.h 6KB
qrc_Login.cpp 1KB
ui_Phonebook.h 8KB
ui_Login.h 6KB
ui_Signup.h 6KB
ui_Usermanage.h 5KB
Phonebook.cpp 9KB
Phonebook.h 833B
Signup.ui 4KB
Mytelephonebook.vcxproj 11KB
Login.qrc 61B
Login.cpp 2KB
Addcontact.ui 4KB
Signup.h 338B
Addcontact.h 393B
Usermanage.ui 3KB
.gitignore 6KB
共 30 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功