Qt Address Book (2) Add addresses, qt Address Book add addresses
In the last Qt Address Book (I) interface design, we mainly implemented the address book interface and used the layout manager to layout elements, it also explains the concepts of "subclass" and "ownership.
In this article, we will add three QPushButton buttons on the interface to save the Name and Address content through the "signal-slot function" mechanism.
1. Three buttons
Add three buttons to the right of AddressBook, named "Add", "Submit", and "Cancel" respectively"
1.1 data members
The three buttons and AddressBook are "included" Links (has-a). Therefore, they can be declared as data members of AddressBook.
QPushButton *add_btn_; QPushButton *submit_btn_;
QPushButton *cancel_btn_;
To save the input Name and Address, a data member of the QMap <QString, QString> type is declared.
QMap<QString, QString> contacts_;
To save the content before Name and Address, declare two QString data members.
QString old_name_; QString old_addr_;
1.2 Layout
Create QVBoxLayout layout_btn, combine the three buttons into a column, and then add layout_btn to layout of the main layout manager.
// three btn add_btn_ = new QPushButton(tr("Add")); submit_btn_ = new QPushButton(tr("Submit")); cancel_btn_ = new QPushButton(tr("Cancel")); // new layout_btn QVBoxLayout *layout_btn = new QVBoxLayout; layout_btn->addWidget(add_btn_, Qt::AlignTop); layout_btn->addWidget(submit_btn_); layout_btn->addWidget(cancel_btn_); layout_btn->addStretch(); // add layout_btn into layout layout->addLayout(layout_btn,1,2);
1.3 addStrech () function
Is the layout manager. The difference between calling the addStretch () function and not calling
2. Function Description of the signal slot 2.1
1) Add function
QLineEdit and QTextEdit are read-only by default. If you click Add, the two statuses change to editable. In this case, you can enter the Name and Address content. At the same time, the Submit and Cancel buttons are displayed.
2) Submit Function
Click the Submit button to save the Name and Address entered by the user to the program. If the input is empty, the system prompts you to enter the information. If the Name has been added, the system prompts you to add
3) Cancel Function
Click the "Cancel" button to Cancel the Name and Address entered by the user. If this parameter is left blank, the previous Name and Address are displayed.
2.2 signal Slot Mechanism
The signal slot mechanism is mainly used for communication between class objects and is the essence of Qt. It is similar to the observer mode and callback mechanism.
When a specific event occurs, the corresponding signal is sent, connected to the slot function of the signal, and then called
2.3 connect function
Use the connect function to connect the Class Object + signal of the sent signal and the class Object + slot function of the received signal.
The specific implementation code is as follows:
connect(add_btn_, SIGNAL(clicked()), this, SLOT(OnAdd()));connect(submit_btn_, SIGNAL(clicked()), this, SLOT(OnSubmit()));connect(cancel_btn_, SIGNAL(clicked()), this, SLOT(OnCancel()));
3 slot Functions
The header file declares the Three slot functions as follows:
public slots: void OnAdd(); void OnSubmit(); void OnCancel();
3.1 OnAdd () function
Void AddressBook: OnAdd () {old_name _ = name_line _-> text (); // Save the previous Name and Address old_addr _ = addr_text _-> toPlainText (); name_line _-> clear (); addr_text _-> clear (); name_line _-> setReadOnly (false); // set QLineEdit and QTextEdit to editable name_line _-> setFocus (Qt:: OtherFocusReason); addr_text _-> setReadOnly (false); add_btn _-> setEnabled (false); // display the Submit button and Cancel button submit_btn _-> show (); cancel_btn _-> show ();}
3.2 OnSubmit () function
void AddressBook::OnSubmit(){ QString name = name_line_->text(); QString address = addr_text_->toPlainText(); if (name.isEmpty() || address.isEmpty()) { QMessageBox::information(this, tr("Empty Field"), tr("Please enter a name and address.")); return; } 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)); return; } if (contacts_.isEmpty()) { name_line_->clear(); addr_text_->clear(); } name_line_->setReadOnly(true); addr_text_->setReadOnly(true); add_btn_->setEnabled(true); submit_btn_->hide(); cancel_btn_->hide();}
3.3 OnCancel () function
void AddressBook::OnCancel(){ name_line_->setText(old_name_); name_line_->setReadOnly(true); addr_text_->setText(old_addr_); addr_text_->setReadOnly(true); add_btn_->setEnabled(true); submit_btn_->hide(); cancel_btn_->hide();}
References:
Qt 5.9 | Qt Widgets | Part 2-Adding Addresses