| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 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.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \example dialogs/extension
|
|---|
| 44 | \title Extension Example
|
|---|
| 45 |
|
|---|
| 46 | The Extension example shows how to add an extension to a QDialog
|
|---|
| 47 | using the QAbstractButton::toggled() signal and the
|
|---|
| 48 | QWidget::setVisible() slot.
|
|---|
| 49 |
|
|---|
| 50 | \image extension-example.png Screenshot of the Extension example
|
|---|
| 51 |
|
|---|
| 52 | The Extension application is a dialog that allows the user to
|
|---|
| 53 | perform a simple search as well as a more advanced search.
|
|---|
| 54 |
|
|---|
| 55 | The simple search has two options: \gui {Match case} and \gui
|
|---|
| 56 | {Search from start}. The advanced search options include the
|
|---|
| 57 | possibilities to search for \gui {Whole words}, \gui {Search
|
|---|
| 58 | backward} and \gui {Search selection}. Only the simple search is
|
|---|
| 59 | visible when the application starts. The advanced search options
|
|---|
| 60 | are located in the application's extension part, and can be made
|
|---|
| 61 | visible by pressing the \gui More button:
|
|---|
| 62 |
|
|---|
| 63 | \image extension_more.png Screenshot of the Extension example
|
|---|
| 64 |
|
|---|
| 65 | \section1 FindDialog Class Definition
|
|---|
| 66 |
|
|---|
| 67 | The \c FindDialog class inherits QDialog. The QDialog class is the
|
|---|
| 68 | base class of dialog windows. A dialog window is a top-level
|
|---|
| 69 | window mostly used for short-term tasks and brief communications
|
|---|
| 70 | with the user.
|
|---|
| 71 |
|
|---|
| 72 | \snippet examples/dialogs/extension/finddialog.h 0
|
|---|
| 73 |
|
|---|
| 74 | The \c FindDialog widget is the main application widget, and
|
|---|
| 75 | displays the application's search options and controlling
|
|---|
| 76 | buttons.
|
|---|
| 77 |
|
|---|
| 78 | In addition to a constructor, we declare the several child
|
|---|
| 79 | widgets: We need a QLineEdit with an associated QLabel to let the
|
|---|
| 80 | user type a word to search for, we need several \l
|
|---|
| 81 | {QCheckBox}{QCheckBox}es to facilitate the search options, and we
|
|---|
| 82 | need three \l {QPushButton}{QPushButton}s: the \gui Find button to
|
|---|
| 83 | start a search and the \gui More button to enable an advanced search.
|
|---|
| 84 | Finally, we need a QWidget representing the application's extension
|
|---|
| 85 | part.
|
|---|
| 86 |
|
|---|
| 87 | \section1 FindDialog Class Implementation
|
|---|
| 88 |
|
|---|
| 89 | In the constructor we first create the standard child widgets for
|
|---|
| 90 | the simple search: the QLineEdit with the associated QLabel, two
|
|---|
| 91 | of the \l {QCheckBox}{QCheckBox}es and all the \l
|
|---|
| 92 | {QPushButton}{QPushButton}s.
|
|---|
| 93 |
|
|---|
| 94 | \snippet examples/dialogs/extension/finddialog.cpp 0
|
|---|
| 95 |
|
|---|
| 96 | We give the options and buttons a shortcut key using the &
|
|---|
| 97 | character. In the \gui {Find what} option's case, we also need to
|
|---|
| 98 | use the QLabel::setBuddy() function to make the shortcut key work
|
|---|
| 99 | as expected; then, when the user presses the shortcut key
|
|---|
| 100 | indicated by the label, the keyboard focus is transferred to the
|
|---|
| 101 | label's buddy widget, the QLineEdit.
|
|---|
| 102 |
|
|---|
| 103 | We set the \gui Find button's default property to true, using the
|
|---|
| 104 | QPushButton::setDefault() function. Then the push button will be
|
|---|
| 105 | pressed if the user presses the Enter (or Return) key. Note that a
|
|---|
| 106 | QDialog can only have one default button.
|
|---|
| 107 |
|
|---|
| 108 | \snippet examples/dialogs/extension/finddialog.cpp 2
|
|---|
| 109 |
|
|---|
| 110 | Then we create the extension widget, and the \l
|
|---|
| 111 | {QCheckBox}{QCheckBox}es associated with the advanced search
|
|---|
| 112 | options.
|
|---|
| 113 |
|
|---|
| 114 | \snippet examples/dialogs/extension/finddialog.cpp 3
|
|---|
| 115 |
|
|---|
| 116 | Now that the extension widget is created, we can connect the \gui
|
|---|
| 117 | More button's \l{QAbstractButton::toggled()}{toggled()} signal to
|
|---|
| 118 | the extension widget's \l{QWidget::setVisible()}{setVisible()} slot.
|
|---|
| 119 |
|
|---|
| 120 | The QAbstractButton::toggled() signal is emitted whenever a
|
|---|
| 121 | checkable button changes its state. The signal's argument is true
|
|---|
| 122 | if the button is checked, or false if the button is unchecked. The
|
|---|
| 123 | QWidget::setVisible() slot sets the widget's visible status. If
|
|---|
| 124 | the status is true the widget is shown, otherwise the widget is
|
|---|
| 125 | hidden.
|
|---|
| 126 |
|
|---|
| 127 | Since we made the \gui More button checkable when we created it,
|
|---|
| 128 | the connection makes sure that the extension widget is shown
|
|---|
| 129 | depending on the state of \gui More button.
|
|---|
| 130 |
|
|---|
| 131 | We also put the check boxes associated with the advanced
|
|---|
| 132 | search options into a layout we install on the extension widget.
|
|---|
| 133 |
|
|---|
| 134 | \snippet examples/dialogs/extension/finddialog.cpp 4
|
|---|
| 135 |
|
|---|
| 136 | Before we create the main layout, we create several child layouts
|
|---|
| 137 | for the widgets: First we allign the QLabel ans its buddy, the
|
|---|
| 138 | QLineEdit, using a QHBoxLayout. Then we vertically allign the
|
|---|
| 139 | QLabel and QLineEdit with the check boxes associated with the
|
|---|
| 140 | simple search, using a QVBoxLayout. We also create a QVBoxLayout
|
|---|
| 141 | for the buttons. In the end we lay out the two latter layouts and
|
|---|
| 142 | the extension widget using a QGridLayout.
|
|---|
| 143 |
|
|---|
| 144 | \snippet examples/dialogs/extension/finddialog.cpp 5
|
|---|
| 145 |
|
|---|
| 146 | Finally, we hide the extension widget using the QWidget::hide()
|
|---|
| 147 | function, making the application only show the simple search
|
|---|
| 148 | options when it starts. When the user wants to access the advanced
|
|---|
| 149 | search options, the dialog only needs to change the visibility of
|
|---|
| 150 | the extension widget. Qt's layout management takes care of the
|
|---|
| 151 | dialog's appearance.
|
|---|
| 152 | */
|
|---|