Changeset 561 for trunk/doc/src/tutorials/addressbook.qdoc
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/doc/src/tutorials/addressbook.qdoc
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information (qt-info@nokia.com) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation (qt-info@nokia.com) 5 6 ** 6 7 ** This file is part of the documentation of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you have questions regarding the use of this file, please contact 37 ** Nokia at qt-info@nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 44 44 45 45 \startpage {index.html}{Qt Reference Documentation} 46 \contentspage Tutorials 46 47 \nextpage {tutorials/addressbook/part1}{Chapter 1} 47 48 48 49 \title Address Book Tutorial 49 \ingroup howto50 \ingroup tutorials51 50 \brief An introduction to GUI programming, showing how to put together a 52 51 simple yet fully-functioning application. … … 223 222 Notice that \c addressLabel is positioned using Qt::AlignTop as an 224 223 additional argument. This is to make sure it is not vertically centered in 225 cell (1,0). For a basic overview on Qt Layouts, refer to the \l{Layout Classes}226 document.224 cell (1,0). For a basic overview on Qt Layouts, refer to the 225 \l{Layout Management} documentation. 227 226 228 227 In order to install the layout object onto the widget, we have to invoke … … 243 242 \snippet tutorials/addressbook/part1/main.cpp main function 244 243 245 We construct a new \c AddressBook widget on the heap using the \c new246 keyword and invokeits \l{QWidget::show()}{show()} function to display it.244 We construct a new \c AddressBook widget on the stack and invoke 245 its \l{QWidget::show()}{show()} function to display it. 247 246 However, the widget will not be shown until the application's event loop 248 247 is started. We start the event loop by calling the application's 249 248 \l{QApplication::}{exec()} function; the result returned by this function 250 is used as the return value from the \c main() function. 249 is used as the return value from the \c main() function. At this point, 250 it becomes apparent why we instanciated \c AddressBook on the stack: It 251 will now go out of scope. Therefore, \c AddressBook and all its child widgets 252 will be deleted, thus preventing memory leaks. 251 253 */ 252 254 … … 297 299 We also declare two private QString objects, \c oldName and \c oldAddress. 298 300 These objects are needed to hold the name and address of the contact that 299 was last displayed, before the user clicked "Add". So, when the user clicks300 "Cancel", we can revert to displaying the details of the last contact.301 was last displayed, before the user clicked \gui Add. So, when the user clicks 302 \gui Cancel, we can revert to displaying the details of the last contact. 301 303 302 304 \section1 Implementing the AddressBook Class … … 304 306 Within the constructor of \c AddressBook, we set the \c nameLine and 305 307 \c addressText to read-only, so that we can only display but not edit 306 existing co tact details.308 existing contact details. 307 309 308 310 \dots … … 319 321 {show()} function, while the \c submitButton and \c cancelButton are 320 322 hidden by invoking \l{QPushButton::hide()}{hide()}. These two push 321 buttons will only be displayed when the user clicks "Add"and this is323 buttons will only be displayed when the user clicks \gui Add and this is 322 324 handled by the \c addContact() function discussed below. 323 325 … … 363 365 \o We extract the contact's details from \c nameLine and \c addressText 364 366 and store them in QString objects. We also validate to make sure that the 365 user did not click "Submit"with empty input fields; otherwise, a367 user did not click \gui Submit with empty input fields; otherwise, a 366 368 QMessageBox is displayed to remind the user for a name and address. 367 369 … … 375 377 376 378 If the contact already exists, again, we display a QMessageBox to inform 377 the user about this, to preventthe user from adding duplicate contacts.378 Our \c contacts object is based on key-value pairs of name and address es,379 the user about this, preventing the user from adding duplicate contacts. 380 Our \c contacts object is based on key-value pairs of name and address, 379 381 hence, we want to ensure that \e key is unique. 380 382 … … 397 399 \snippet tutorials/addressbook/part2/addressbook.cpp cancel 398 400 399 The general idea to add a contact is to give the user the flexibility to400 click "Submit" or "Cancel" at any time. The flowchart below further401 explains this concept:401 The general idea behind adding a contact is to give the user the 402 flexibility to click \gui Submit or \gui Cancel at any time. The flowchart below 403 further explains this concept: 402 404 403 405 \image addressbook-tutorial-part2-add-flowchart.png … … 455 457 456 458 The image below is our expected graphical user interface. Notice that it 457 is getting closer to our expected final output.459 is getting closer to our final application. 458 460 459 461 \image addressbook-tutorial-part3-screenshot.png … … 511 513 \o If the iterator is at the end of \c contacts, we clear the 512 514 display and return. 513 \o If the iterator is the beginning of \c contacts, we move it to515 \o If the iterator is at the beginning of \c contacts, we move it to 514 516 the end. 515 517 \o We then decrement the iterator by one. … … 530 532 \title Address Book 4 - Editing and Removing Addresses 531 533 532 In this chapter, we look at ways to modify the contents of contact stored534 In this chapter, we look at ways to modify the contents of contacts stored 533 535 in the address book application. 534 536 … … 540 542 when needed. However, this requires a little improvement, in the form of 541 543 enums. In our previous chapters, we had two modes: \c{AddingMode} and 542 \c{NavigationMode} - but they were n't defined as enums. Instead, we544 \c{NavigationMode} - but they were not defined as enums. Instead, we 543 545 enabled and disabled the corresponding buttons manually, resulting in 544 546 multiple lines of repeated code. … … 574 576 \snippet tutorials/addressbook/part4/addressbook.h mode declaration 575 577 576 Lastly, we declare \c currentMode to keep track of the current mode of the 577 enum. 578 Lastly, we declare \c currentMode to keep track of the enum's current mode. 578 579 579 580 \section1 Implementing the AddressBook Class … … 645 646 \snippet tutorials/addressbook/part4/addressbook.cpp update interface() part 1 646 647 647 For \c NavigationMode, however, we include conditions within the 648 parameters of the QPushButton::setEnabled(). This is to ensure that649 the \c editButton and \c removeButton push buttons are enabled when there650 is at least one contact in the address book; \c nextButton and \c previousButton651 are onlyenabled when there is more than one contact in the address book.648 For \c NavigationMode, however, we include conditions within the parameters 649 of the QPushButton::setEnabled() function. This is to ensure that 650 \c editButton and \c removeButton are enabled when there is at least one 651 contact in the address book; \c nextButton and \c previousButton are only 652 enabled when there is more than one contact in the address book. 652 653 653 654 \snippet tutorials/addressbook/part4/addressbook.cpp update interface() part 2 … … 696 697 \snippet tutorials/addressbook/part5/finddialog.h FindDialog header 697 698 698 We define a public function, \c getFindText() for use by classes that 699 instantiate \c FindDialog, which allows them to obtain the text 700 entered by the user. A public slot, \c findClicked(), is defined to 701 handle the search string when the user clicks the \gui Find button. 699 We define a public function, \c getFindText(), to be used by classes that 700 instantiate \c FindDialog. This function allows these classes to obtain the 701 search string entered by the user. A public slot, \c findClicked(), is also 702 defined to handle the search string when the user clicks the \gui Find 703 button. 702 704 703 705 Lastly, we define the private variables, \c findButton, \c lineEdit … … 714 716 \snippet tutorials/addressbook/part5/finddialog.cpp constructor 715 717 716 We set the layout and window title, as well as connect the signals 717 to their respective slots. Notice that \c{findButton}'s718 \l{QPushButton::clicked()}{clicked()} signal is connected to to719 \ c findClicked() and \l{QDialog::accept()}{accept()}. The720 \l{QDialog::accept()}{accept()} slot provided by QDialog hides721 the dialog and sets the result code to \l{QDialog::}{Accepted}.722 We use this function to help \c{AddressBook}'s \c findContact() function723 know when the \c FindDialog object has been closed. This will be724 further explained when discussing the\c findContact() function.718 We set the layout and window title, as well as connect the signals to their 719 respective slots. Notice that \c{findButton}'s \l{QPushButton::clicked()} 720 {clicked()} signal is connected to to \c findClicked() and 721 \l{QDialog::accept()}{accept()}. The \l{QDialog::accept()}{accept()} slot 722 provided by QDialog hides the dialog and sets the result code to 723 \l{QDialog::}{Accepted}. We use this function to help \c{AddressBook}'s 724 \c findContact() function know when the \c FindDialog object has been 725 closed. We will explain this logic in further detail when discussing the 726 \c findContact() function. 725 727 726 728 \image addressbook-tutorial-part5-signals-and-slots.png … … 816 818 \image addressbook-tutorial-part6-screenshot.png 817 819 818 Although browsing and searching for contacts are useful features, our address 819 book is not really fully ready for use until we can saving existing contacts 820 and load them again at a later time. 821 Qt provides a number of classes for \l{Input/Output and Networking}{input and output}, 822 but we have chosen to use two which are simple to use in combination: QFile and 823 QDataStream. 824 825 A QFile object represents a file on disk that can be read from and written to. 826 QFile is a subclass of the more general QIODevice class which represents many 827 different kinds of devices. 828 829 A QDataStream object is used to serialize binary data so that it can be stored 830 in a QIODevice and retrieved again later. Reading from a QIODevice and writing 831 to it is as simple as opening the stream - with the respective device as a 832 parameter - and reading from or writing to it. 820 Although browsing and searching for contacts are useful features, our 821 address book is not ready for use until we can save existing contacts and 822 load them again at a later time. 823 824 Qt provides a number of classes for \l{Input/Output and Networking} 825 {input and output}, but we have chosen to use two which are simple to use 826 in combination: QFile and QDataStream. 827 828 A QFile object represents a file on disk that can be read from and written 829 to. QFile is a subclass of the more general QIODevice class which 830 represents many different kinds of devices. 831 832 A QDataStream object is used to serialize binary data so that it can be 833 stored in a QIODevice and retrieved again later. Reading from a QIODevice 834 and writing to it is as simple as opening the stream - with the respective 835 device as a parameter - and reading from or writing to it. 836 833 837 834 838 \section1 Defining the AddressBook Class … … 872 876 \image addressbook-tutorial-part6-save.png 873 877 874 If \c fileName is not empty, we create a QFile object, \c file with878 If \c fileName is not empty, we create a QFile object, \c file, with 875 879 \c fileName. QFile works with QDataStream as QFile is a QIODevice. 876 880 … … 902 906 903 907 If \c fileName is not empty, again, we use a QFile object, \c file, and 904 attempt to open it in \l{QIODevice::}{ReadOnly} mode. In a similar way905 to our implementation of \c saveToFile(), if this attempt is unsuccessful,906 wedisplay a QMessageBox to inform the user.908 attempt to open it in \l{QIODevice::}{ReadOnly} mode. Similar to our 909 implementation of \c saveToFile(), if this attempt is unsuccessful, we 910 display a QMessageBox to inform the user. 907 911 908 912 \snippet tutorials/addressbook/part6/addressbook.cpp loadFromFile() function part2 … … 910 914 Otherwise, we instantiate a QDataStream object, \c in, set its version as 911 915 above and read the serialized data into the \c contacts data structure. 912 Note that we empty \c contacts before reading data into it to simplify the913 file reading process. A more advanced method would be to read the contacts914 into temporary QMap object, and copy only the contacts that do not already915 exist in\c contacts.916 The \c contacts object is emptied before data is read into it to simplify 917 the file reading process. A more advanced method would be to read the 918 contacts into a temporary QMap object, and copy over non-duplicate contacts 919 into \c contacts. 916 920 917 921 \snippet tutorials/addressbook/part6/addressbook.cpp loadFromFile() function part3
Note:
See TracChangeset
for help on using the changeset viewer.