#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("通讯录管理系统");
setWindowFlags(Qt::Window);
contactDialog = new ContactDialog(this);
contactList = new QList<Contact*>();
QTableWidget *tableWidget = ui->centralwidget->findChild<QTableWidget*>("tableWidget");
tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
connect(ui->actionadd, &QAction::triggered, this, [=]() {
qDebug() << "add";
contactDialog->setWindowTitle(QString("添加联系人"));
int status = contactDialog->exec();
if (status == QDialog::Accepted) {
Contact *contact = getDialogContact();
contact->setSeq(contactList->size());
contactList->insert(contactList->size(), contact);
}
contactDialog->clear();
refreshTable();
});
connect(ui->actionedit, &QAction::triggered, this, [=]() {
qDebug() << "edit";
Contact *contact = contactList->at(tableWidget->currentRow());
contactDialog->setName(contact->getName());
contactDialog->setUnit(contact->getUnit());
contactDialog->setPhone(contact->getPhone());
contactDialog->setEmail(contact->getEmail());
contactDialog->initEditReadOnly(true);
contactDialog->setWindowTitle(QString("修改联系人"));
int status = contactDialog->exec();
if (status == QDialog::Accepted) {
Contact *item = getDialogContact();
contactList->replace(contact->getSeq(), item);
}
contactDialog->clear();
refreshTable();
});
connect(ui->actiondelete, &QAction::triggered, this, [=]() {
qDebug() << "deleted";
qDebug() << "size: " + QString(tableWidget->currentRow());
contactList->removeAt(tableWidget->currentRow());
refreshTable();
});
connect(ui->centralwidget->findChild<QPushButton*>("resetBtn"), &QPushButton::clicked, this, [=]() {
QLineEdit *nameSearch = ui->centralwidget->findChild<QLineEdit*>("nameSearch");
QLineEdit *phoneSearch = ui->centralwidget->findChild<QLineEdit*>("phoneSearch");
qDebug() << "clear";
nameSearch->clear();
phoneSearch->clear();
});
connect(ui->centralwidget->findChild<QPushButton*>("searchBtn"), &QPushButton::clicked, this, [=]() {
qDebug() << "search";
refreshTable();
});
tableWidget->horizontalHeader()->setStretchLastSection(true); // 设置充满表宽度
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
tableWidget->setColumnCount(4);
readContact();
refreshTable();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::refreshTable()
{
QTableWidget *tableWidget = ui->centralwidget->findChild<QTableWidget*>("tableWidget");
QLineEdit *nameSearch = ui->centralwidget->findChild<QLineEdit*>("nameSearch");
QLineEdit *phoneSearch = ui->centralwidget->findChild<QLineEdit*>("phoneSearch");
QList<Contact*> *filterContactList = new QList<Contact*>();
for (int i = 0; i < contactList->size(); i ++) {
Contact *contact = contactList->at(i);
if (!nameSearch->text().trimmed().isEmpty()) {
qDebug() << "name search:" << nameSearch->text().trimmed();
if (contact->getName().isEmpty() | !contact->getName().contains(nameSearch->text().trimmed(), Qt::CaseSensitive)) {
continue;
}
qDebug() << "add name";
}
if (!phoneSearch->text().trimmed().isEmpty()) {
qDebug() << "phone search:" << phoneSearch->text().trimmed();
if (contact->getPhone().isEmpty() | !contact->getPhone().contains(phoneSearch->text().trimmed(), Qt::CaseSensitive)) {
continue;
}
qDebug() << "add phone";
}
filterContactList->insert(filterContactList->size(), contact);
qDebug() << "show data:" << contact->getName() << ":" << contact->getUnit() << ":" << contact->getPhone() << ":" << contact->getEmail();
}
tableWidget->setRowCount(filterContactList->size());
for (int i = 0; i < filterContactList->size(); i ++) {
Contact *contact = filterContactList->at(i);
tableWidget->setItem(i, 0, new QTableWidgetItem(contact->getName()));
tableWidget->setItem(i, 1, new QTableWidgetItem(contact->getUnit()));
tableWidget->setItem(i, 2, new QTableWidgetItem(contact->getPhone()));
tableWidget->setItem(i, 3, new QTableWidgetItem(contact->getEmail()));
qDebug() << "data:" << contact->getName() << ":" << contact->getUnit() << ":" << contact->getPhone() << ":" << contact->getEmail();
}
writeContact();
}
Contact* MainWindow::getDialogContact()
{
QString name = contactDialog->getName();
QString unit = contactDialog->getUnit();
QString phone = contactDialog->getPhone();
QString email = contactDialog->getEmail();
Contact *contact = new Contact(name, unit, phone, email);
return contact;
}
void MainWindow::readContact()
{
QFile file("contact.txt");
if (!file.open(QIODevice::ReadOnly))
return;
contactList->clear();
int i = 0;
while (!file.atEnd()) {
QByteArray line = file.readLine();
QList<QByteArray> contactByteList = line.replace("\n", "").split(',');
qDebug() << "read: " << QString(line) << ":" << contactByteList.size();
Contact *contact = new Contact(QString(contactByteList.at(0)), QString(contactByteList.at(1)), QString(contactByteList.at(2)), QString(contactByteList.at(3)));
contact->setSeq(i);
i ++;
contactList->insert(i, contact);
}
qDebug() << "read complete contact size: " << contactList->size();
}
void MainWindow::writeContact()
{
QFile file("contact.txt");
if (!file.open(QIODevice::WriteOnly))
return;
QTextStream out(&file);
qDebug() << "contact size: " << contactList->size();
for (int i = 0; i < contactList->size(); i ++) {
Contact *contact = contactList->at(i);
QString *contactString = new QString;
contactString->append(contact->getName()).append(",");
contactString->append(contact->getUnit()).append(",");
contactString->append(contact->getPhone()).append(",");
contactString->append(contact->getEmail()).append("\n");
qDebug() << contactString->toUtf8();
out << contactString->toUtf8();
}
}
基于qt的简单联系人系统.zip
需积分: 0 100 浏览量
更新于2023-12-31
收藏 11KB ZIP 举报
QT,C++使用技巧,实战应用开发小系统参考资料,源码参考。
详细介绍了一些Qt框架的各种功能和模块,以及如何使用Qt进行GUI开发、网络编程和跨平台应用开发等。
适用于初学者和有经验的开发者,能够帮助你快速上手Qt并掌握其高级特性。