Qt地址簿-加个信号及槽

addressbook.h:
 
#ifndef __ADDRESSBOOK_H__
#define __ADDRESSBOOK_H__

// 地址簿
#include <QWidget>
#include <QMap>

class QLineEdit;
class QTextEdit;
class QPushButton;

class Address: public QWidget
{
  Q_OBJECT

public:
  Address(QWidget* parent=0);
public slots:
   void addContact();
   void submitContact();
   void cancel();
    
private:
  QPushButton* addButton;
  QPushButton* submitButton;
  QPushButton* cancelButton;
  QLineEdit *nameLine;
  QTextEdit *addressText;
    
  QString oldName;
  QString oldAddress;
    
  QMap<QString,QString> contacts;
};

#endif // __ADDRESSBOOK_H__
 
 
addressbook.cpp:
 
#include <QtGui>

#include "addressbook.h"

Address::Address(QWidget* parent):QWidget(parent)
{
  QLabel *nameLabel= new QLabel( "Name:");
  nameLine= new QLineEdit();
  nameLine->setReadOnly( true);
    
  QLabel *addressLabel= new QLabel( "Address:");
  addressText= new QTextEdit;
  addressText->setReadOnly( true);
    
  addButton= new QPushButton( "&Add");
  addButton->show();    
    
  submitButton= new QPushButton( "&Submit");
  submitButton->hide();    
    
  cancelButton= new QPushButton( "&Cancel");
  cancelButton->hide();    
        
  connect(addButton,SIGNAL(clicked()), this,SLOT(addContact()));
  connect(submitButton,SIGNAL(clicked()), this,SLOT(submitContact()));
  connect(cancelButton,SIGNAL(clicked()), this,SLOT(cancel()));
    
  QVBoxLayout* btnLayout= new QVBoxLayout();
  btnLayout->addWidget(addButton);
  btnLayout->addWidget(submitButton);
  btnLayout->addWidget(cancelButton);
  btnLayout->addStretch();
    
  QGridLayout *mainLayout= new QGridLayout;
  mainLayout->addWidget(nameLabel,0,0);
  mainLayout->addWidget(nameLine,0,1);
  mainLayout->addWidget(addressLabel,1,0,Qt::AlignTop);
  mainLayout->addWidget(addressText,1,1);
  mainLayout->addLayout(btnLayout,1,2);
    
  setLayout(mainLayout);
  setWindowTitle( "Simple Address Book");
}

void Address::addContact()
{
   //保存旧值
  oldName=nameLine->text();
  oldAddress=addressText->toPlainText();
    
   //清空文本框
  nameLine->clear();
  addressText->clear();    
    
   //可读写
  nameLine->setReadOnly( false);
  addressText->setReadOnly( false);
    
   //设置显示状态
  addButton->setEnabled( false);
  submitButton->show();
  cancelButton->show();
}

//提交输入的信息
void Address::submitContact()
{
  QString name=nameLine->text();
  QString address=addressText->toPlainText();
    
   if(name== "" || address==""){
    QMessageBox::information( this, "Empty Field", "Please Enter a name or address!");
     return;
  }
    
   if(!contacts.contains(name)){
    contacts.insert(name,address); //插入数据
    QMessageBox::information( this, "Add Successful", "OK!");
  } else{
    QMessageBox::information( this, "Add UnSuccessful", "Sorry!");
     return;
  }
    
   //设为只读
  nameLine->setReadOnly( true);
  addressText->setReadOnly( true);
    
   //设置按钮显示状态
  addButton->setEnabled( true);
  submitButton->hide();
  cancelButton->hide();    
}
void Address::cancel()
{
   //文本框设置为旧值
  nameLine->setText(oldName);
  nameLine->setReadOnly( true);
    
   //设置旧值
  addressText->setText(oldAddress);
  addressText->setReadOnly( true);
    
  addButton->setEnabled( true);
  submitButton->hide();
  cancelButton->hide();
}

 
 
main.cpp:
 
#include <QApplication>
#include "addressbook.h"

int main( int argc, char *argv[])
{
        QApplication app(argc, argv);

        Address *addressBook = new Address;
        addressBook->show();

         return app.exec();
}

你可能感兴趣的:(C++,GUI,职场,qt,休闲)