QT实现地址薄的功能,熟悉下QT的基本操作,对QT有所帮助
0.png (42.67 KB, 下载次数: 140)
下载附件
保存到相册
2016-4-7 01:29 上传
全部源码下载:
qt地址薄源码.rar
(506.1 KB, 下载次数: 6)
2016-4-7 01:31 上传
点击文件名下载附件
下载积分: 黑币 -5
部分源码预览:
#include "addressbook.h"
#include "ui_addressbook.h"
AddressBook::AddressBook(QWidget *parent) :
QWidget(parent),
ui(new Ui::AddressBook)
{
ui->setupUi(this);
ui->nameLine->setReadOnly(true);
ui->addressText->setReadOnly(true);
ui->submitButton->hide();
ui->cancelButton->hide();
ui->nextButton->setEnabled(false);
ui->previousButton->setEnabled(false);
ui->editButton->setEnabled(false);
ui->removeButton->setEnabled(false);
ui->findButton->setEnabled(false);
connect(ui->addButton,SIGNAL(clicked()),this,SLOT(addContact()));
connect(ui->submitButton,SIGNAL(clicked()),this,SLOT(submitContact()));
connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(cancel()));
connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(next()));
connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(previous()));
connect(ui->editButton, SIGNAL(clicked()), this, SLOT(editContact()));
connect(ui->removeButton, SIGNAL(clicked()), this, SLOT(removeContact()));
connect(ui->findButton, SIGNAL(clicked()), this, SLOT(findContact()));
setWindowTitle(tr("地址薄--汪佐"));
}
AddressBook::~AddressBook()
{
delete ui;
}
void AddressBook::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type())
{
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void AddressBook::addContact()
{
oldName = ui->nameLine->text();
oldAddress = ui->addressText->toPlainText();
ui->nameLine->clear();
ui->addressText->clear();
updateInterface(AddingMode);
}
void AddressBook::submitContact()
{
QString name = ui->nameLine->text();
QString address = ui->addressText->toPlainText();
if (name == "" || address == "")
{
QMessageBox::information(this, tr("空的"), tr("请输入姓名或地址."));
return ;
}
if (currentMode == AddingMode)
{
if (!contacts.contains(name))
{
contacts.insert(name, address);
QMessageBox::information(this, tr("添加成功"), tr("\"%1\" 已经添加。").arg(name));
}
else
{
QMessageBox::information(this, tr("添加失败"), tr("\"%1\" 已经存在。").arg(name));
return;
}
}
else
if (currentMode == EditingMode)
{
if (oldName != name)
{
if (!contacts.contains(name))
{
QMessageBox::information(this, tr("修改成功1"), tr("\"%1\" 已经修改。").arg(oldName));
contacts.remove(oldName);
contacts.insert(name, address);
}
}
else if (oldAddress != address)
{
QMessageBox::information(this, tr("修改成功2"), tr("\"%1\" 已经修改。").arg(name));
contacts[name] = address;
}
else
{
QMessageBox::information(this, tr("无法修改"), tr("\"%1\" 已经存在。").arg(oldName));//防止修改后重名
return;
}
}
updateInterface(NavigationMode);
if (contacts.isEmpty())
{
ui->nameLine->clear();
ui->addressText->clear();
}
int number = contacts.size();
ui->nextButton->setEnabled(number > 1);
ui->previousButton->setEnabled(number > 1);
ui->nameLine->setReadOnly(true);
ui->addressText->setReadOnly(true);
ui->addButton->setEnabled(true);
ui->submitButton->hide();
ui->cancelButton->hide();
}
void AddressBook::cancel()
{
ui->nameLine->setText(oldName);
ui->nameLine->setReadOnly(true);
ui->addressText->setText(oldAddress);
updateInterface(NavigationMode);
}
void AddressBook::next()
{
QString name = ui->nameLine->text();
QMap<QString, QString>::iterator i = contacts.find(name);
if (i != contacts.end())
{
i++;
}
if (i == contacts.end())
{
i = contacts.begin();
}
ui->nameLine->setText(i.key());
ui->addressText->setText(i.value());
}
void AddressBook::previous()
{
QString name = ui->nameLine->text();
QMap<QString, QString>::iterator i = contacts.find(name);
if (i == contacts.end())
{
ui->nameLine->clear();
ui->addressText->clear();
return;
}
if (i == contacts.begin())
{
i = contacts.end();
}
i--;
ui->nameLine->setText(i.key());
ui->addressText->setText(i.value());
}
void AddressBook::editContact()
{
oldName = ui->nameLine->text();
oldAddress = ui->addressText->toPlainText();
updateInterface(EditingMode);
}
void AddressBook::removeContact()
{
QString name = ui->nameLine->text();
QString address = ui->addressText->toPlainText();
if (contacts.contains(name))
{
int button = QMessageBox::question(this,tr(&quot;删除确认&quot;),tr(&quot;确认要删除 \&quot;%1\&quot;?&quot;).arg(name), QMessageBox::Yes | QMessageBox::No);
if (button == QMessageBox::Yes)
{
previous();
contacts.remove(name);
QMessageBox::information(this, tr(&quot;删除成功&quot;),tr(&quot;\&quot;%1\&quot; 已经删除。&quot;).arg(name));
}
}
updateInterface(NavigationMode);
}
void AddressBook::updateInterface(Mode mode)
{
currentMode = mode;
switch (currentMode)
{
case AddingMode:
case EditingMode:
ui->nameLine->setReadOnly(false);
ui->nameLine->setFocus(Qt::OtherFocusReason);
ui->addressText->setReadOnly(false);
ui->addButton->setEnabled(false);
ui->editButton->setEnabled(false);
ui->removeButton->setEnabled(false);
ui->nextButton->setEnabled(false);
ui->previousButton->setEnabled(false);
ui->submitButton->show();
ui->cancelButton->show();
break;
case NavigationMode:
if (contacts.isEmpty())
{
ui->nameLine->clear();
ui->addressText->clear();
}
ui->nameLine->setReadOnly(true);
ui->addressText->setReadOnly(true);
ui->addButton->setEnabled(true);
int number = contacts.size();
ui->editButton->setEnabled(number >= 1);
ui->removeButton->setEnabled(number >= 1);
ui->nextButton->setEnabled(number > 1);
ui->previousButton->setEnabled(number >1);
ui->submitButton->hide();
ui->cancelButton->hide();
ui->findButton->setEnabled(number > 2);
break;
}
}
void AddressBook::findContact()
{
FindDialog *dialog = new FindDialog;
if (dialog->exec() == QDialog::Accepted)
{
QString contactName = dialog->findText();
if (contacts.contains(contactName))
{
ui->nameLine->setText(contactName);
ui->addressText->setText(contacts.value(contactName));
}
else
{
QMessageBox::information(this, tr(&quot;未找到联系人&quot;),tr(&quot;地址簿中没有\&quot;%1\&quot; 。&quot;).arg(contactName));
return;
}
}
updateInterface(NavigationMode);
}
复制代码 |
|