Qt地址簿-加个信号及槽


所属类别:开发技术

文章作者:xcf007

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


addressbook.h:#ifndef __ADDRESSBOOK_H__#define __ADDRESSBOOK_H__// 地址簿#include #include class QLineEdit;class QTextEdit;class QPushButton;class Address:public QWidget{Q_OBJECTpublic: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 contacts;};#endif // __ADDRESSBOOK_H__addressbook.cpp:#include #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 #include "addressbook.h"int main(int argc, char *argv[]){QApplication app(argc, argv);Address *addressBook = new Address;addressBook->show();return app.exec();}本文出自 51CTO.COM技术博客

相关信息

· [转]Linux下查看某个端口下运行的是什么程序

· 关于禁止缓存,答案很多,我发现ms的做法是

· LiMo PK Android:一山能否容二虎?

· MSN病毒原理及测试代码








....

17683 503