Example 2 of QT (4) signal, SLOT, and QMap-Addressbook

Source: Internet
Author: User

Based on the previous MeeGo developer (V): QT (3) object and inheritance small example, we add three buttons, refer to http://doc.qt.nokia.com/latest/tutorials-addressbook-part2.html
, Region:

  1. The three buttons are placed on layout and placed on guidlayout.
  2. When you press these buttons, release signal, which triggers some slot functions. This is the focus of this study.
  3. Learn how to use qmap.

The last UI. We placed a layout on GuidLayou (), with three buttons on layout. Press Add to Add a new contact and press submit to Add the contact, click Cancel to Cancel adding the contact. On the right side of the figure, we also see three button placement methods. We will explain them in the program.

User information is stored in QMap. QMap is suitable for storing indexed data. In this example, the name is used as the index and the address is the information corresponding to the person's name. QMap is a good way to store databases without using external databases.

After a button is clicked, release a signal. We need to call this signal and a function slot function on QT. In this example, we need to establish three corresponding concerns ,. Method:Connect(Addbutton/* SIGNAL object */, SIGNAL (clicked ()/* capture SIGNAL */, this,Slot(AddContact ()/* slot function triggered after signal monitoring */);

You do not need to modify main. cpp. addressbook. h is as follows:

# Ifndef com_wei_addressbook_h
# Define com_wei_addressbook_h

# Include <qwidget>
# Include <QMap>

Class qlineedit;
Class qlabel;
Class qtextedit;
Class qpushbutton;

Class addressbook: Public qwidget
{
Q_object

Public:
Addressbook (qwidget * parent = NULL );

/* A slot is a function that responds to a participant signal .*/
Public slots:
Void addcontact ();
Void submitcontact ();
Void cancel ();

PRIVATE:
Qpushbutton * addbutton, * submitbutton, * cancelbutton;
Qlineedit * nameline;
Qtextedit * addresstext;

/* Contacts is used to store contact information. It is a qmap object and a key-value storage. Here, the contact name is used as the key and the contact address is used as the value .*/
Qmap contacts;
QString oldName, oldAddress;
};

# Endif

Addressbook. cpp is as follows:

/* Addressbook. cpp-the implementation file for the AddressBook class */

# Include <QtGuid>
# Include "addressbook. h"

AddressBook: AddressBook (QWidget * parent): QWidget (parent)
{
QLabel * nameLabel = new QLabel (tr ("Name :"));
NameLine = new QLineEdit ();
NameLine->SetReadOnly(True); // not editable

QLabel * addressLabel = new QLabel (tr ("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 ();

QVBoxLayout * buttonLayout1 = new QVBoxLayout;
Buttonlayout1-> addwidget (addbutton, QT: aligntop );
Buttonlayout1-> addwidget (submitbutton );
Buttonlayout1-> addwidget (cancelbutton );
Buttonlayout1->AddStretch(); // Compact the layout. Otherwise, the layout is divided into three equal points, that is, the right figure in the preceding UI diagram.

Connect (addButton, SIGNAL (Clicked ()), This, SLOT (AddContact ()));
Connect (submitButton, SIGNAL (Clicked ()), This, SLOT (SubmitContact ()));
Connect (cancelButton, SIGNAL (Clicked ()), This, SLOT (Cancel ()));

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(ButtonLayout1, 1, 2 );

SetLayout (mainLayout );
SetWindowTitle (tr ("Simple Address Book "));
}

Void AddressBook: addContact ()
{
Printf ("Line % d: % s/n" ,__ LINE __,__ FUNCTION __);
OldName = nameLine-> text ();
OldAddress = addressText-> toPlainText ();

NameLine-> clear ();
AddressText-> clear ();

NameLine-> setReadOnly (false );
NameLine-> setFocus (Qt: OtherFocusReason );
AddressText-> setReadOnly (false );

AddButton-> setEnabled (false );
SubmitButton-> show ();
CancelButton-> show ();
}

Void AddressBook: submitContact ()
{
Printf ("Line % d: % s/n" ,__ LINE __,__ FUNCTION __);
QString name = nameLine-> text ();
QString address = addressText-> toPlainText ();
If (name. isEmpty () | address. isEmpty ()){
QMessageBox: information(This, tr ("Empty Field"), tr ("Please enter a name and address "));
Return;
}

If (contacts. contains (name )){
QMessageBox: information (this, tr ("Add Unsuccessful! "), Tr (" Sorry,/"% 1/" is already in your address book "). arg (name ));
Return;
}

Contacts.Insert(Name, address );
// QMap usage
QMessageBox: information(This, tr ("Add Successful! "),
Tr("<% 1, % 2> Has been added to your address book ."). Arg (name, address)); // % 1, % 2 indicates the Parameter order in arg, as shown in the figure below

NameLine-> setReadOnly (true );
AddressText-> setReadOnly (true );
AddButton-> setEnabled (true );
SubmitButton-> hide ();
CancelButton-> hide ();
}

Void AddressBook: cancel ()
{
Printf ("Line % d: % s/n" ,__ LINE __,__ FUNCTION __);

NameLine-> setText (oldName );
NameLine-> setReadOnly (true );
AddressText-> setText (oldAddress );
AddressText-> setReadOnly (true );

AddButton-> setEnabled (true );
SubmitButton-> hide ();
CancelButton-> hide ();
}

The QMessageBox is as follows:

We will proceed with this process. We add two buttons for viewing forward or backward, practice the layout again, and learn a qmap storage. The entry of qmap is <key, value>. The key arrangement is unclear. However, in the experiment, we can see that if we read the elements of qmap at one time, it will be read in order (letter size order. Begin is the first element, but end is not the last element. end may be null. Therefore, end-1 is the last valid element. The iterator of qmap can perform ++ and -- operations.

The basic operations for data are add, delete, modify, and query. We have added two functions: Submit and cancel. The buttons are common. The editabled and disenditabled created under different functional requirements, enabled/disenable and show/hide have different display requirements. We use an updateui function to process statistics and use an Enum to differentiate different modes. Ui independence and functionality are also one of the development principles.

For addressbook. H, we add related button definitions and slot functions.

......
Class AddressBook: public QWidget
{
......
Public:
......
EnumMode {navigationmode, addingmode, editingmode}; // sets the enumeration mode.

Public slots:
......
Void next ();
Void previous ();
Void editcontact ();
Void removecontact ();

PRIVATE:
......
Qpushbutton * nextbutton, * previusbutton;
Qpushbutton * editbutton, * removebutton;
Mode currentmode;
Void updateui (mode); // a function dedicated to UI Processing
};
......

Modify the addressbook. cpp file as follows to make the program look more elegant:

......
Addressbook: addressbook (qwidget * parent): qwidget (parent)
{
......
EditButton = new QPushButton (tr ("& Edit "));
EditButton-> setEnabled (false );
RemoveButton = new QPushButton (tr ("& Remove "));
RemoveButton-> setEnabled (false );
......
ButtonLayout1-> addWidget (editButton );
// Place the edit and remove buttons in front of the sumbit and calcel buttons.
ButtonLayout1-> addWidget (removeButton );
ButtonLayout1-> addWidget (submitButton );
ButtonLayout1-> addWidget (cancelButton );

......
Connect (editButton, SIGNAL (clicked (), this, SLOT (editContact ()));
Connect (removeButton, SIGNAL (clicked (), this, SLOT (removeContact ()));
......
NextButton = new QPushButton (tr ("& Next "));
NextButton-> setEnabled (false );
Previusbutton = new QPushButton (tr ("& Previous "));
Previusbutton-> setEnabled (false );
Connect (nextButton, SIGNAL (clicked (), this, SLOT (next ()));
Connect (previusbutton, SIGNAL (clicked (), this, SLOT (previous ()));
QHBoxLayout * buttonLayout2 = new QHBoxLayout;
ButtonLayout2-> addWidget (nextButton );
ButtonLayout2-> addWidget (previusbutton );
......
MainLayout-> addLayout (buttonLayout1, 1, 2 );
MainLayout-> addLayout (buttonLayout2, 2, 1 );
......
}

Void AddressBook: updateUI (Mode mode ){
Currentmode = mode;
Switch (mode ){
Case addingmode:
Case editingmode:
If (mode = addingmode)
Editbutton-> hide ();
Else if (mode = editingmode)
Addbutton-> hide ();
Removebutton-> hide ();

Nameline-> setreadonly (false );
Nameline->SetFocus(QT: otherfocusreason );
Addresstext-> setreadonly (false );

AddButton-> setEnabled (false );
EditButton-> setEnabled (false );
RemoveButton-> setEnabled (false );
NextButton-> setEnabled (false );
Previusbutton-> setEnabled (false );

SubmitButton-> show ();
CancelButton-> show ();
Break;
Case NavigationMode:
// Browsing mode: enters the browsing mode after any function is completed. If there is an element, the next and previous are valid, and you can enter the function operations in this mode.
NameLine-> setReadOnly (true );
AddressText-> setReadOnly (true );
If (contacts. isEmpty ()){
NameLine-> clear ();
AddressText-> clear ();
}
AddButton-> setEnabled (true );
EditButton-> setEnabled (! Contacts. isEmpty ());
RemoveButton-> setEnabled (! Contacts. isEmpty ());
NextButton-> setEnabled (! Contacts. isEmpty ());
Previusbutton-> setEnabled (! Contacts. isEmpty ());
AddButton-> show ();
EditButton-> show ();
RemoveButton-> show ();
SubmitButton-> hide ();
CancelButton-> hide ();
Break;
Default:
Break;
}
}

Void AddressBook: addContact ()
{
OldName = nameLine-> text ();
OldAddress = addressText-> toPlainText ();
NameLine-> clear ();
Addresstext-> clear ();
Updateui (addingmode );
}

Void addressbook: editcontact (){
Oldname = nameline-> text ();
Oldaddress = addresstext-> toplaintext ();
Updateui (editingmode );
}

Void addressbook: removecontact (){
Qstring name = nameline-> text ();
Qstring address = addresstext-> toplaintext ();

If (contacts. Contains (name )){
If (qmessagebox ::Question(This, TR ("Confirm remove"), TR ("are you sure you want to remove % 1"). Arg (name ),
QMessageBox: Yes | QMessageBox: No) = QMessageBox: Yes ){
Previous ();
Contacts.Remove(Name); // QMap operation
If (contacts. isEmpty ()){
NameLine-> clear ();
AddressText-> clear ();
}
QMessageBox: information (this, tr ("Remove successful! "),
TR ("/" % 1/"is already removed from your address book"). Arg (name ));
}
}
Updateui (navigationmode );
}

Void addressbook: submitcontact ()
{
Qstring name = nameline-> text ();
Qstring address = addresstext-> toplaintext ();

If (name. isempty () | Address. isempty ()){
Qmessagebox: Information (this, TR ("empty field "),
TR ("Please enter a name and address "));
Return;
}

Switch (currentMode ){
Case AddingMode:
If (contacts. contains (name )){
QMessageBox: information (this, tr ("Add Unsuccessful! "), Tr (" Sorry,/"% 1/" is already in your address book "). arg (name ));
Return;
} Else {
Contacts.Insert(Name, address); // QMap usage: add
QMessageBox: information (this, tr ("Add Successful! "), Tr (" <% 1, % 2> has been added. "). arg (name, address ));
}
Break;
Case EditingMode:
If (oldName! = Name ){
If (! Contacts. contains (name )){
QMessageBox: information (this, tr ("Edit successful! "), Tr (" Sorry, <% 1> has been edited. "). arg (name ));
Contacts.Remove(OldName); // QMap usage: Delete
Contacts.Insert(Name, address );
// QMap usage: added
} Else {
Qmessagebox: Information (this, TR ("Edit unsuccessful! "), TR (" sorry, <% 1> already in your addressbook. "). Arg (name ));
Cancel ();
}
} Else if (oldaddress! = Address ){
Qmessagebox: Information (this, TR ("Edit successful! "), TR (" sorry, <% 1> has been edited. "). Arg (name ));
Contacts [name] = address;
// You can use an array-like method to assign values to qmap [Key ].
Cancel ();
} Break;
Default:
Break;
}
UpdateUI (NavigationMode );
}

Void AddressBook: cancel ()
{
NameLine-> setText (oldName );
AddressText-> setText (oldAddress );
UpdateUI (NavigationMode );
}

Void AddressBook: next ()
{
QString name = nameLine-> text ();
QMap <QString, QString> ::IteratorI = contacts.Find(Name); // QMap lookup and interator

I ++; // you can use I ++ to move an element to the next element.

If (I = contacts. end () // since the last element is NULL, it does not actually exist and needs to be set as the first element.
I = contacts. Begin ();

Nameline-> settext (I. Key (); // obtain the key value
Addresstext-> settext (I. Value (); // obtain the value

}

Void addressbook: Previous ()
{
Qstring name = nameline-> text ();
Qmap <qstring, qstring >:: iterator I = contacts. Find (name );

If (I = contacts. Begin () {// if it is the first element, you need to move it to the actual last element, that is, end-1.
I = contacts. End ()-1;
} Else {
I --;
}

Nameline-> settext (I. Key ());
Addresstext-> settext (I. Value ());
}

Related Links: My meego/moblin articles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.