Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346405
  • 博文数量: 92
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 960
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-21 19:38
文章分类

全部博文(92)

文章存档

2010年(71)

2009年(21)

我的朋友

分类: 嵌入式

2010-05-18 19:12:57

Qt Simple Address Book

学习到三个知识点:

1. 通过模式设置,来实现模式切换带来的界面控制

addressbook.h 中:

我们声明 currentMode 来跟踪 enum 的当前模式。

public:

     AddressBook(QWidget *parent = 0);

     enum Mode { NavigationMode, AddingMode, EditingMode };

 

private:

         void updateInterface(Mode mode);

         Mode currentMode;

 

addressbook.cpp中:

为了在模式间切换,我们引入了 updateInterface() 函数来控制所有 对象的启用和禁用。

void AddressBook::addContact()

 {

     oldName = nameLine->text();

     oldAddress = addressText->toPlainText();

 

     nameLine->clear();

     addressText->clear();

 

     updateInterface(AddingMode);

 }

 void AddressBook::editContact()

 {

     oldName = nameLine->text();

     oldAddress = addressText->toPlainText();

 

     updateInterface(EditingMode);

 }

if (currentMode == AddingMode)

else if (currentMode == EditingMode)

 

void AddressBook::updateInterface(Mode mode)

 {

     currentMode = mode;

 

     switch (currentMode) {

 

     case AddingMode:

     case EditingMode:

 

         nameLine->setReadOnly(false);

         nameLine->setFocus(Qt::OtherFocusReason);

         addressText->setReadOnly(false);

 

         addButton->setEnabled(false);

         editButton->setEnabled(false);

         removeButton->setEnabled(false);

 

         nextButton->setEnabled(false);

         previousButton->setEnabled(false);

 

         submitButton->show();

         cancelButton->show();

         break;

     case NavigationMode:

 

         if (contacts.isEmpty()) {

             nameLine->clear();

             addressText->clear();

         }

 

         nameLine->setReadOnly(true);

         addressText->setReadOnly(true);

         addButton->setEnabled(true);

 

         int number = contacts.size();

         editButton->setEnabled(number >= 1);

         removeButton->setEnabled(number >= 1);

         nextButton->setEnabled(number > 1);

         previousButton->setEnabled(number >1 );

 

         submitButton->hide();

         cancelButton->hide();

         break;

     }

 

2. 使用QMap来做简单的数据库管理

Contacts对象是基于nameaddress的键-值对应关系。

contacts[name] = address;

addressbook.h 中:

#include

private:

     QMap contacts;

     QString oldName;

     QString oldAddress;

addressbook.cpp中:

void AddressBook::submitContact()

 {

     QString name = nameLine->text();

     QString address = addressText->toPlainText();

 

     if (name == "" || address == "") {

         QMessageBox::information(this, tr("Empty Field"),

             tr("Please enter a name and address."));

     }

     if (currentMode == AddingMode) {

 

         if (!contacts.contains(name)) {

             contacts.insert(name, address);

             QMessageBox::information(this, tr("Add Successful"),

                 tr("\"%1\" has been added to your address book.").arg(name));

         } else {

             QMessageBox::information(this, tr("Add Unsuccessful"),

                 tr("Sorry, \"%1\" is already in your address book.").arg(name));

         }

     } else if (currentMode == EditingMode) {

 

         if (oldName != name) {

             if (!contacts.contains(name)) {

                 QMessageBox::information(this, tr("Edit Successful"),

                     tr("\"%1\" has been edited in your address book.").arg(oldName));

                 contacts.remove(oldName);

                 contacts.insert(name, address);

             } else {

                 QMessageBox::information(this, tr("Edit Unsuccessful"),

                     tr("Sorry, \"%1\" is already in your address book.").arg(name));

             }

         } else if (oldAddress != address) {

             QMessageBox::information(this, tr("Edit Successful"),

                 tr("\"%1\" has been edited in your address book.").arg(name));

             contacts[name] = address;

         }

     }

 

     updateInterface(NavigationMode);

 }


void AddressBook::next()

 {

     QString name = nameLine->text();

     QMap::iterator i = contacts.find(name);

 

     if (i != contacts.end())

         i++;

 

     if (i == contacts.end())

         i = contacts.begin();

 

     nameLine->setText(i.key());

     addressText->setText(i.value());

 }

}

 

3. 通过按键,新起一个对话框

 

addressbook.h 中:

#include "finddialog.h"

private:

         FindDialog *dialog;

 

addressbook.cpp中:

findButton = new QPushButton(tr("&Find"));

findButton->setEnabled(false);

dialog = new FindDialog;

connect(findButton, SIGNAL(clicked()), this, SLOT(findContact()));

 

 

void AddressBook::findContact()

 {

     dialog->show();

 

//Qdialog的等待返回机制:

     if (dialog->exec() == QDialog::Accepted) {

         QString contactName = dialog->getFindText();

 

         if (contacts.contains(contactName)) {

             nameLine->setText(contactName);

             addressText->setText(contacts.value(contactName));

         } else {

             QMessageBox::information(this, tr("Contact Not Found"),

                 tr("Sorry, \"%1\" is not in your address book.").arg(contactName));

             return;

         }

     }

 

     updateInterface(NavigationMode);

 }

finddialog.cpp中:

#include

 #include "finddialog.h"

 

 FindDialog::FindDialog(QWidget *parent)

     : QDialog(parent)

 {

     QLabel *findLabel = new QLabel(tr("Enter the name of a contact:"));

     lineEdit = new QLineEdit;

 

     findButton = new QPushButton(tr("&Find"));

     findText = "";

 

     QHBoxLayout *layout = new QHBoxLayout;

     layout->addWidget(findLabel);

     layout->addWidget(lineEdit);

     layout->addWidget(findButton);

 

     setLayout(layout);

     setWindowTitle(tr("Find a Contact"));

     connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));

     connect(findButton, SIGNAL(clicked()), this, SLOT(accept()));

 }

 void FindDialog::findClicked()

 {

     QString text = lineEdit->text();

 

     if (text.isEmpty()) {

         QMessageBox::information(this, tr("Empty Field"),

             tr("Please enter a name."));

         return;

     } else {

         findText = text;

         lineEdit->clear();

         hide();

     }

 }

 QString FindDialog::getFindText()  //这是一个公共函数,在addressbook.cpp中也可以调用

 

 {

     return findText;

 

 }

 

阅读(1819) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~