Записная книжка Диаграмма состояний интерфейса Объектная
Записная книжка
Диаграмма состояний интерфейса
Объектная декомпозиция
Главная форма
Файл mainform. h #ifndef main. Form_h #define main. Form_h #include
Файл main. Form. cpp #include "main. Form. h" Window: : Window() { QText. Codec *codec = QText. Codec: : codec. For. Name("Windows-1251"); this->set. Window. Title(RUS("Записная книжка")); btn. Add = new QPush. Button(RUS("Добавить"), this); btn. Find = new QPush. Button(RUS("Найти"), this); btn. Print = new QPush. Button(RUS("Показать"), this); btn. Exit = new QPush. Button(RUS("Выход"), this); QVBox. Layout *layout = new QVBox. Layout(this); layout->add. Widget(btn. Add); layout->add. Widget(btn. Find); layout->add. Widget(btn. Print); layout->add. Widget(btn. Exit);
Файл main. Form. cpp (2) resize(180, 150); connect(btn. Exit, SIGNAL(clicked(bool)), this, SLOT(close())); connect(btn. Add, SIGNAL(clicked(bool)), this, SLOT(show. Add())); connect(btn. Find, SIGNAL(clicked(bool)), this, SLOT(show. Find())); connect(btn. Print, SIGNAL(clicked(bool)), this, SLOT(show. Print())); } void Window: : show. Add() { win. Add. show(); } void Window: : show. Find() { win. Find. show(); } void Window: : show. Print() { win. Print. show. All(); }
Форма добавления записей
Файл add. Form. h #ifndef add. Form_h #define add. Form_h #include
Файл add. Form. cpp #include "add. Form. h" #include "book. File. h" #include "main. Form. h" add. Form: : add. Form() { QText. Codec *codec = QText. Codec: : codec. For. Name("Windows-1251"); this->set. Window. Title(RUS("Добавление записей")); family=new QLabel(RUS("Фамилия"), this); name=new QLabel(RUS("Имя"), this); nomer=new QLabel(RUS("Телефон"), this); QVBox. Layout *layout. V 1 = new QVBox. Layout(); layout. V 1 ->add. Widget(family); layout. V 1 ->add. Widget(name); layout. V 1 ->add. Widget(nomer);
Файл add. Form. cpp (2) QVBox. Layout *layout. V 2 = new QVBox. Layout(); family. Edit=new QLine. Edit(RUS(""), this); name. Edit=new QLine. Edit(RUS(""), this); nomer. Edit=new QLine. Edit(RUS(""), this); layout. V 2 ->add. Widget(family. Edit); layout. V 2 ->add. Widget(name. Edit); layout. V 2 ->add. Widget(nomer. Edit); QHBox. Layout *layout. G 1 = new QHBox. Layout(); layout. G 1 ->add. Layout(layout. V 1); layout. G 1 ->add. Layout(layout. V 2); QHBox. Layout *layout. G 2 = new QHBox. Layout(); btn. Add=new QPush. Button(RUS("Добавить"), this); btn. Exit=new QPush. Button(RUS("Назад"), this); layout. G 2 ->add. Widget(btn. Add); layout. G 2 ->add. Widget(btn. Exit);
Файл add. Form. cpp (3) QVBox. Layout *layout = new QVBox. Layout(this); layout->add. Layout(layout. G 1); layout->add. Layout(layout. G 2); connect(btn. Add, SIGNAL(clicked(bool)), this, SLOT(add. Record())); connect(btn. Exit, SIGNAL(clicked(bool)), this, SLOT(close())); } void add. Form: : add. Record() { book. File book; rec. Type r; r. fam=family. Edit->text(); r. name=name. Edit->text(); r. nom=nomer. Edit->text(); family. Edit->clear(); name. Edit->clear(); nomer. Edit->clear(); book. add. Rec(r); }
Диаграмма класса файла
Файл book. File. h #ifndef book. File_h #define book. File_h #include
Файл book. File. cpp #include "book. File. h" #include "main. Form. h" book. File: : book. File() { QText. Codec *codec = QText. Codec: : codec. For. Name("Windows- 1251"); f=new QFile("book. txt"); if(!f->exists()) { QMessage. Box msg(QMessage. Box: : Critical, RUS("Файл не найден"), RUS("Файл book. txt будет создан"), QMessage. Box: : Ok, 0); msg. exec(); } f->open(QFile: : Read. Write); } book. File: : ~book. File() { f->close(); delete f; }
Файл book. File. cpp (2) bool book. File: : add. Rec(rec. Type r) { f->seek(f->size()); QData. Stream out(f); out<
Файл book. File. cpp (3) bool book. File: : find. First(const rec. Type r 1) { k 1=(r 1. fam==""); // устанавливаем два ключа поиска k 2=(r 1. name==""); ff=false; // ключ поиска «запись не найдена» f->reset(); bool fff = read. Rec(); while(fff &&(!ff)) { k 3=(r 1. fam==r. fam); //строим еще два ключа поиска k 4=(r 1. name==r. name); if ((!k 1 && !k 2 && k 3 && k 4)|| (!k 1 && k 2 && k 3)||(k 1 && !k 2 && k 4)) ff=true; // ключ поиска «запись найдена» else fff=read. Rec(); } return ff; // возвращаем ключ поиска }
Файл book. File. cpp (4) bool book. File: : find. Next(const rec. Type r 1) { ff=false; // ключ поиска «запись не найдена» bool fff = read. Rec(); while((!ff) && fff) { k 3=(r 1. fam==r. fam); //строим еще два ключа поиска k 4=(r 1. name==r. name); if ((!k 1 && !k 2 && k 3 && k 4)|| (!k 1 && k 2 && k 3)||(k 1 && !k 2 && k 4)) ff=true; // ключ поиска «запись найдена» else fff=read. Rec(); } return ff; // возвращаем ключ поиска }
Форма вывода результатов
Файл print. Form. h #ifndef print. Form_h #define print. Form_h #include
Файл print. Form. cpp #include "print. Form. h" #include "main. Form. h" print. Form: : print. Form() { codec = QText. Codec: : codec. For. Name("Windows-1251"); this->set. Window. Title(RUS("Результат")); QString. List strlist; strlist <
Файл print. Form. cpp(2) void print. Form: : show. All() { book. File book; if (!book. read. Rec()) { QMessage. Box msg(QMessage. Box: : Critical, RUS("Нет данных"), RUS("База пуста"), QMessage. Box: : Ok, 0); msg. exec(); } else show. Row(0, book. r); int i=0; while (book. read. Rec()) { show. Row(++i, book. r); } table->set. Row. Count(i+1); resize(350, 330); show(); }
Файл print. Form. cpp(3) void print. Form: : show. Results(rec. Type r 1) { book. File book; if (!book. find. First(r 1)) { QMessage. Box msg(QMessage. Box: : Critical, RUS("Нет данных"), RUS("Данные не найдены"), QMessage. Box: : Ok, 0); msg. exec(); } else { show. Row(0, book. r); int i=0; while (book. find. Next(r 1))show. Row(++i, book. r); table->set. Row. Count(i+1); resize(350, 200); show(); } }
Файл print. Form. cpp(3) void print. Form: : show. Row(int i, rec. Type r) { QTable. Widget. Item *item; item = new QTable. Widget. Item(); item->set. Flags(Qt: : No. Item. Flags); item->set. Text(r. fam); table->set. Item(i, 0, item); item = new QTable. Widget. Item(); item->set. Flags(Qt: : No. Item. Flags); item->set. Text(r. name); table->set. Item(i, 1, item); item = new QTable. Widget. Item(); item->set. Flags(Qt: : No. Item. Flags); item->set. Text(r. nom); table->set. Item(i, 2, item); }
Форма поиска записей
Файл find. Form. h #ifndef find. Form_h #define find. Form_h #include
Файл find. Form. cpp #include "find. Form. h" #include "book. File. h" #include "main. Form. h" find. Form: : find. Form() { QText. Codec *codec = QText. Codec: : codec. For. Name("Windows-1251"); this->set. Window. Title(RUS("Поиск записей")); QVBox. Layout *layout. V 1 = new QVBox. Layout(); family=new QLabel(RUS("Фамилия"), this); name=new QLabel(RUS("Имя"), this); layout. V 1 ->add. Widget(family); layout. V 1 ->add. Widget(name); QVBox. Layout *layout. V 2 = new QVBox. Layout(); family. Edit=new QLine. Edit(RUS(""), this); name. Edit=new QLine. Edit(RUS(""), this); layout. V 2 ->add. Widget(family. Edit); layout. V 2 ->add. Widget(name. Edit);
Файл find. Form. cpp (2) QHBox. Layout *layout. G 1 = new QHBox. Layout(); layout. G 1 ->add. Layout(layout. V 1); layout. G 1 ->add. Layout(layout. V 2); QHBox. Layout *layout. G 2 = new QHBox. Layout(); btn. Find=new QPush. Button(RUS("Найти"), this); btn. Exit=new QPush. Button(RUS("Назад"), this); layout. G 2 ->add. Widget(btn. Find); layout. G 2 ->add. Widget(btn. Exit); QVBox. Layout *layout = new QVBox. Layout(this); layout->add. Layout(layout. G 1); layout->add. Layout(layout. G 2); connect(btn. Find, SIGNAL(clicked(bool)), this, SLOT(find. Recs())); connect(btn. Exit, SIGNAL(clicked(bool)), this, SLOT(close())); }
Файл find. Form. cpp (3) void find. Form: : find. Recs() { rec. Type r; r. fam=family. Edit->text(); r. name=name. Edit->text(); win. Print. show. Results(r); }
Файл main. cpp #include "main. Form. h" int main(int argc, char *argv[]) { QApplication appl(argc, argv); Window win; win. show(); return appl. exec(); }
Записная книжка.ppt
- Количество слайдов: 30

