~bzoltan/kubuntu-packaging/decouple_cmake_plugin

« back to all changes in this revision

Viewing changes to doc/addressbook-sdk.qdoc

  • Committer: Timo Jyrinki
  • Date: 2013-11-15 12:25:23 UTC
  • mfrom: (1.1.28)
  • Revision ID: timo.jyrinki@canonical.com-20131115122523-i2kyamsu4gs2mu1m
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    Qt, such as:
36
36
 
37
37
    \list
38
 
        \o  Widgets and layout managers
39
 
        \o  Container classes
40
 
        \o  Signals and slots
41
 
        \o  Input and output devices
 
38
        \li  Widgets and layout managers
 
39
        \li  Container classes
 
40
        \li  Signals and slots
 
41
        \li  Input and output devices
42
42
    \endlist
43
43
 
44
44
    All these technologies will be introduced via the Qt Creator Integrated
53
53
    Tutorial chapters:
54
54
 
55
55
    \list 1
56
 
        \o \l{examples/addressbook-sdk/part1}{Designing the User Interface}
57
 
        \o \l{examples/addressbook-sdk/part2}{Adding Addresses}
58
 
        \o \l{examples/addressbook-sdk/part3}{Navigating between Entries}
59
 
        \o \l{examples/addressbook-sdk/part4}{Editing and Removing Addresses}
60
 
        \o \l{examples/addressbook-sdk/part5}{Adding a Find Function}
61
 
        \o \l{examples/addressbook-sdk/part6}{Loading and Saving}
62
 
        \o \l{examples/addressbook-sdk/part7}{Additional Features}
 
56
        \li \l{examples/addressbook-sdk/part1}{Designing the User Interface}
 
57
        \li \l{examples/addressbook-sdk/part2}{Adding Addresses}
 
58
        \li \l{examples/addressbook-sdk/part3}{Navigating between Entries}
 
59
        \li \l{examples/addressbook-sdk/part4}{Editing and Removing Addresses}
 
60
        \li \l{examples/addressbook-sdk/part5}{Adding a Find Function}
 
61
        \li \l{examples/addressbook-sdk/part6}{Loading and Saving}
 
62
        \li \l{examples/addressbook-sdk/part7}{Additional Features}
63
63
    \endlist
64
64
 
65
65
    Although this little application does not look much like a fully-fledged
93
93
    Begin by launching Qt Creator and use it to generate a new project. To
94
94
    do this, select \gui{File} > \gui{New File or Project...} >
95
95
    \gui{Qt Application Project} > \gui{Qt Gui Application} and
96
 
    click \gui OK. Set your project name to \bold part1 with the QtCore and
 
96
    click \gui OK. Set your project name to \b part1 with the QtCore and
97
97
    QtGui modules checked. Ensure that you select QWidget as your base class
98
98
    and name it \c AddressBook.
99
99
 
101
101
    \gui{Project}:
102
102
 
103
103
    \list
104
 
        \o  \c{main.cpp} - the file containing a \c main() function, with an
105
 
            instance of \c AddressBook,
106
 
        \o  \c{addressbook.cpp} - the implementation file for the
107
 
            \c AddressBook class,
108
 
        \o  \c{addressbook.h} - the definition file for the \c AddressBook
109
 
            class,
110
 
        \o  \c{addressbook.ui} - the user interface file created with \QD,
111
 
            and
112
 
        \o  \c{part1.pro} - the project file.
 
104
        \li  \c{main.cpp} - the file containing a \c main() function, with an
 
105
             instance of \c AddressBook,
 
106
        \li  \c{addressbook.cpp} - the implementation file for the
 
107
             \c AddressBook class,
 
108
        \li  \c{addressbook.h} - the definition file for the \c AddressBook
 
109
             class,
 
110
        \li  \c{addressbook.ui} - the user interface file created with \QD,
 
111
             and
 
112
        \li  \c{part1.pro} - the project file.
113
113
    \endlist
114
114
 
115
115
    Now that you have all the files you need, click \gui Finish so you can
132
132
    QLineEdit and a QTextEdit for the input fields. To create this follow the
133
133
    steps mentioned below:
134
134
    \list
135
 
       \o  Drag those widgets from the \gui{Widget Box} to your form.
136
 
       \o  In the \gui{Property Editor}, set their \gui{objectName} property to
137
 
           \c nameLabel and \c addressLabel for the \l{QLabel}s, \c nameLine
138
 
           for the QLineEdit and finally, \c addressText for the QTextEdit.
139
 
       \o  Position the widgets properly, according to the screenshot above.
140
 
       \o  Use a QGridLayout to position our labels and input fields in a
141
 
           structured manner. QGridLayout divides the available space into
142
 
           a grid and places widgets in the cells you specify with row and
143
 
           column numbers.
144
 
       \o  Place the caption of the \c addressLabel on the top, change the
 
135
       \li  Drag those widgets from the \gui{Widget Box} to your form.
 
136
       \li  In the \gui{Property Editor}, set their \gui{objectName} property to
 
137
            \c nameLabel and \c addressLabel for the \l{QLabel}s, \c nameLine
 
138
            for the QLineEdit and finally, \c addressText for the QTextEdit.
 
139
       \li  Position the widgets properly, according to the screenshot above.
 
140
       \li  Use a QGridLayout to position our labels and input fields in a
 
141
            structured manner. QGridLayout divides the available space into
 
142
            a grid and places widgets in the cells you specify with row and
 
143
            column numbers.
 
144
       \li  Place the caption of the \c addressLabel on the top, change the
145
145
           vertical alignment property to \c AlignTop.
146
146
    \endlist
147
147
 
227
227
    the behavior of a widget has the following advantages:
228
228
 
229
229
    \list
230
 
        \o  You can write implementations of virtual or pure virtual functions
231
 
            to obtain exactly what you need, falling back on the base class's
232
 
            implementation when necessary.
233
 
        \o  It allows you to encapsulate parts of the user interface within a
234
 
            class, so that the other parts of the application do not need to
235
 
            know about the individual widgets in the user interface.
236
 
        \o  The subclass can be used to create multiple custom widgets in the
237
 
            same application or library, and the code for the subclass can be
238
 
            reused in other projects.
 
230
        \li  You can write implementations of virtual or pure virtual functions
 
231
             to obtain exactly what you need, falling back on the base class's
 
232
             implementation when necessary.
 
233
        \li  It allows you to encapsulate parts of the user interface within a
 
234
             class, so that the other parts of the application do not need to
 
235
             know about the individual widgets in the user interface.
 
236
        \li   The subclass can be used to create multiple custom widgets in the
 
237
             same application or library, and the code for the subclass can be
 
238
             reused in other projects.
239
239
    \endlist
240
240
 
241
241
    Since Qt does not provided a specific address book widget, you subclass a
272
272
    complete the process of adding a contact. Break the existing layouts by
273
273
    following the steps below:
274
274
    \list
275
 
        \o Select, \gui{Break Layout} from the context menu. You might have to
276
 
           do a \gui{Select All} with \key{Ctrl+A} first..
277
 
        \o Add three push buttons and double-click on each of them to set
278
 
           their text to "Add", "Submit", and "Cancel".
279
 
        \o Set the \c objectName of the buttons to \c addButton,
280
 
           \c submitButton and \c cancelButton respectively.
281
 
        \o A \gui{Vertical Spacer} is required to ensure that the push buttons
282
 
           will be laid out neatly; drag one from the \gui{Widget Box}.
283
 
        \o Lay out these three push buttons and the spacer vertically, by
284
 
           selecting all three of them using \key{Ctrl + click} and selecting
285
 
           \gui{Lay out Vertically} from the context menu. Alternatively you
286
 
           can  use the \key{Ctrl+L} shortcut key.
 
275
        \li Select, \gui{Break Layout} from the context menu. You might have to
 
276
            do a \gui{Select All} with \key{Ctrl+A} first..
 
277
        \li Add three push buttons and double-click on each of them to set
 
278
            their text to "Add", "Submit", and "Cancel".
 
279
        \li Set the \c objectName of the buttons to \c addButton,
 
280
            \c submitButton and \c cancelButton respectively.
 
281
        \li A \gui{Vertical Spacer} is required to ensure that the push buttons
 
282
            will be laid out neatly; drag one from the \gui{Widget Box}.
 
283
        \li Lay out these three push buttons and the spacer vertically, by
 
284
            selecting all three of them using \key{Ctrl + click} and selecting
 
285
            \gui{Lay out Vertically} from the context menu. Alternatively you
 
286
            can  use the \key{Ctrl+L} shortcut key.
287
287
 
288
 
           \note Use the spacer as you do not want the buttons to be evenly
289
 
           spaced, but arranged closer to the top of the widget.
290
 
        \o The figure below shows the difference between using the spacer and
291
 
           not using it.
292
 
           \image addressbook-tutorial-part2-stretch-effects.png
293
 
        \o Select all the objects on the form using, \key{Ctrl+A} and lay them
294
 
           out in a grid.
295
 
        \o Lastly, set the top level widget's layout by right-clicking anywhere
296
 
           on the widget and selecting \gui{Lay out Horizontally} or
297
 
           \gui{Lay out Vertically}.
 
288
            \note Use the spacer as you do not want the buttons to be evenly
 
289
            spaced, but arranged closer to the top of the widget.
 
290
        \li The figure below shows the difference between using the spacer and
 
291
            not using it.
 
292
            \image addressbook-tutorial-part2-stretch-effects.png
 
293
        \li Select all the objects on the form using, \key{Ctrl+A} and lay them
 
294
            out in a grid.
 
295
        \li Lastly, set the top level widget's layout by right-clicking anywhere
 
296
            on the widget and selecting \gui{Lay out Horizontally} or
 
297
            \gui{Lay out Vertically}.
298
298
    \endlist
299
299
 
300
300
    The final design of the form is shown in the screenshot below:
357
357
    \image addressbook-tutorial-part2-signals-and-slots.png
358
358
 
359
359
    Finally, set the window title to "Simple Address Book" using the
360
 
    \l{QWidget::}{setWindowTitle()} function. The tr() method allows us
 
360
    \l{QWidget::}{setWindowTitle()} function. The tr() function allows us
361
361
    to translate user interface strings.
362
362
 
363
363
    \snippet examples/addressbook-sdk/part2/addressbook.cpp window title
376
376
    This function can be divided into three parts:
377
377
 
378
378
    \list 1
379
 
        \o  Extract the contact's detail from \c nameLine and \c addressText
380
 
            and store them in QString objects. Also validate to ensure that
381
 
            you did not click \gui Submit with empty input fields;
382
 
            otherwise, a QMessageBox is displayed to remind you for a name
383
 
            and address.
384
 
 
385
 
            \snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part1
386
 
 
387
 
        \o  Then proceed to check if the contact already exists. If it does
388
 
            not exist, add the contact to \c contacts and display a
389
 
            QMessageBox to inform you about this, preventing you from
390
 
            adding duplicate contacts. Our \c contacts object is based on
391
 
            key-value pairs or name and address, hence, you want to ensure that
392
 
            \e key is unique.
393
 
 
394
 
            \snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part2
395
 
 
396
 
        \o  Once you have handled both cases mentioned above, restore the
397
 
            push buttons to their normal state with the following code:
398
 
 
399
 
            \snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part3
 
379
        \li  Extract the contact's detail from \c nameLine and \c addressText
 
380
             and store them in QString objects. Also validate to ensure that
 
381
             you did not click \gui Submit with empty input fields;
 
382
             otherwise, a QMessageBox is displayed to remind you for a name
 
383
             and address.
 
384
 
 
385
             \snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part1
 
386
 
 
387
        \li  Then proceed to check if the contact already exists. If it does
 
388
             not exist, add the contact to \c contacts and display a
 
389
             QMessageBox to inform you about this, preventing you from
 
390
             adding duplicate contacts. Our \c contacts object is based on
 
391
             key-value pairs or name and address, hence, you want to ensure that
 
392
             \e key is unique.
 
393
 
 
394
             \snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part2
 
395
 
 
396
        \li  Once you have handled both cases mentioned above, restore the
 
397
             push buttons to their normal state with the following code:
 
398
 
 
399
             \snippet examples/addressbook-sdk/part2/addressbook.cpp submitContact part3
400
400
    \endlist
401
401
 
402
402
    The screenshot below shows the QMessageBox object used to display
457
457
    need to traverse the existing contacts. To do so follow the steps
458
458
    mentioned below:
459
459
    \list
460
 
        \o Add two push buttons at the bottom of your application and name
461
 
           them: \gui Next and \gui Previous.
462
 
        \o The buttons' \c objectName should be \c nextButton and
463
 
           \c previousButton, respectively.
464
 
        \o Break your top level layout by right-clicking on \c AddressBook in
465
 
           the \gui{Object Inspector} and then select \gui{Lay out|Break Layout}.
466
 
        \o Place the \gui Next and \gui Previous buttons in a horizontal
467
 
           layout.
468
 
        \o Drag and drop the buttons together with their layout into the
469
 
           existing grid layout.
470
 
        \o Set a top level layout for the widget again.
 
460
        \li Add two push buttons at the bottom of your application and name
 
461
            them: \gui Next and \gui Previous.
 
462
        \li The buttons' \c objectName should be \c nextButton and
 
463
            \c previousButton, respectively.
 
464
        \li Break your top level layout by right-clicking on \c AddressBook in
 
465
            the \gui{Object Inspector} and then select \gui{Lay out|Break Layout}.
 
466
        \li Place the \gui Next and \gui Previous buttons in a horizontal
 
467
            layout.
 
468
        \li Drag and drop the buttons together with their layout into the
 
469
            existing grid layout.
 
470
        \li Set a top level layout for the widget again.
471
471
    \endlist
472
472
 
473
473
    The screenshot below illustrates what you will see as the button layout
523
523
    for \c contacts and then:
524
524
 
525
525
    \list
526
 
        \o If the iterator is not at the end of \c contacts, increment it by
 
526
        \li If the iterator is not at the end of \c contacts, increment it by
527
527
           one.
528
 
        \o If the iterator is at the end of \c contacts, move it to the
529
 
           beginning of \c contacts. This gives an illusion that our QMap
530
 
           is working like a circularly-linked list.
 
528
        \li If the iterator is at the end of \c contacts, move it to the
 
529
            beginning of \c contacts. This gives an illusion that our QMap
 
530
            is working like a circularly-linked list.
531
531
    \endlist
532
532
 
533
533
    \snippet examples/addressbook-sdk/part3/addressbook.cpp next
539
539
    \c contacts and then:
540
540
 
541
541
    \list
542
 
        \o  If the iterator is at the end of \c contacts, clear the display
543
 
            and return.
544
 
        \o  If the iterator is at the beginning of \c contacts, move it to
545
 
            the end.
546
 
        \o  Then decrement the iterator by one.
 
542
        \li  If the iterator is at the end of \c contacts, clear the display
 
543
             and return.
 
544
        \li  If the iterator is at the beginning of \c contacts, move it to
 
545
             the end.
 
546
        \li  Then decrement the iterator by one.
547
547
    \endlist
548
548
 
549
549
    \snippet examples/addressbook-sdk/part3/addressbook.cpp previous
578
578
 
579
579
    In this chapter, define the \c Mode enum with three different values:
580
580
    \list
581
 
        \o \c{NavigationMode}
582
 
        \o \c{AddingMode}
583
 
        \o \c{EditingMode}
 
581
        \li \c{NavigationMode}
 
582
        \li \c{AddingMode}
 
583
        \li \c{EditingMode}
584
584
    \endlist
585
585
 
586
586
    \section1 Placing Widgets on The Form
827
827
    when you enter a contact name to look up. Once you click the
828
828
    dialog's \c findButton, the dialog is hidden and the result code is set to
829
829
    either QDialog::Accepted or QDialog::Rejected by the FindDialog's
830
 
    \c findClicked() method. This ensures that you only search for a contact
 
830
    \c findClicked() function. This ensures that you only search for a contact
831
831
    if you have typed something in the FindDialog's line edit.
832
832
 
833
833
    Then proceed to extract the search string, which in this case is