summaryrefslogtreecommitdiffstats
path: root/examples/widgets/doc/src/extension.qdoc
blob: 8f35e0e196c9cee297a56e520a62b4fe930a0541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \example dialogs/extension
    \title Extension Example
    \ingroup examples-dialogs

    \brief The Extension example shows how to add an extension to a QDialog
    using the QAbstractButton::toggled() signal and the
    QWidget::setVisible() slot.

    \image extension-example.png Screenshot of the Extension example

    The Extension application lets the user add search parameters in
    a dialog and launch a simple or advanced search.

    The simple search has two options: \uicontrol {Match case} and \uicontrol
    {Search from start}. The advanced search offers search for \uicontrol {Whole words},
    \uicontrol {Search backward}, and \uicontrol {Search selection}.  The
    application starts with simple search as the default. Click the \uicontrol More button
    to show the advanced search options:

    \image extension_more.png Screenshot of the Extension example

    \section1 FindDialog Class Definition

    The \c FindDialog class inherits QDialog. QDialog is the
    base class for dialog windows. A dialog window is a top-level
    window mostly used for short-term tasks and brief communications
    with the user.

    \snippet dialogs/extension/finddialog.h 0

    The \c FindDialog widget is the main application widget, and
    displays the application's search options and controlling
    buttons.

    In addition to the constructor, there are several child widgets:

    \list
    \li A QLineEdit with an associated QLabel to let the
    user type a word to search for.
    \li Several \l {QCheckBox}{QCheckBox}es to facilitate the search options.
    \li Three \l {QPushButton}{QPushButton}s:
        \list
        \li the \uicontrol Find button to start a search
        \li the \uicontrol More button to enable an advanced search
        \li a QWidget representing the application's extension part
        \endlist
    \endlist

    \section1 FindDialog Class Implementation

    Create the standard child widgets for the simple search in the constructor:
    the QLineEdit with the associated QLabel, two {QCheckBox}es and all the
    \l {QPushButton}{QPushButton}s.

    \snippet dialogs/extension/finddialog.cpp 0

    This snippet illustrates how you can define a shortcut key
    for a widget. A shortcut should be defined by putting the ampersand
    character (\c &) in front of the letter that should
    become the shortcut.
    For example, for \uicontrol {Find what}, pressing \uicontrol Alt
    and \uicontrol w transfers focus to the QLineEdit widget.
    Shortcuts can also be used for checking on or off a checkmark.
    For example, pressing \uicontrol Alt and \uicontrol c puts the check mark
    on \uicontrol {Match Case} if it was unchecked and vice versa.
    It is the QLabel::setBuddy() method that links a widget to the shortcut
    character if it has been defined.

    Set the \uicontrol Find button's default property to true, using the
    QPushButton::setDefault() function. Then the push button will be
    pressed if the user presses the Enter (or Return) key. Note that a
    QDialog can only have one default button.

    \snippet dialogs/extension/finddialog.cpp 2

    Create the extension widget, and the \l {QCheckBox}{QCheckBox}es associated
    with the advanced search options.

    \snippet dialogs/extension/finddialog.cpp 3

    Now that the extension widget is created, connect the \uicontrol
    More button's \l{QAbstractButton::toggled()}{toggled()} signal to
    the extension widget's \l{QWidget::setVisible()}{setVisible()} slot.

    The QAbstractButton::toggled() signal is emitted whenever a
    checkable button changes its state. The signal's argument is true
    if the button is checked, or false if the button is unchecked. The
    QWidget::setVisible() slot sets the widget's visible status. If
    the status is true the widget is shown, otherwise the widget is
    hidden.

    Since the \uicontrol More button is checkable, the connection makes
    sure that the extension widget is shown depending on the state of
    the \uicontrol More button.

    Create checkboxes associated with the advanced search options in
    a layout installed on the extension widget.

    \snippet dialogs/extension/finddialog.cpp 4

    Before creating the main layout, create several child layouts
    for the widgets. First align the QLabel and its buddy, the
    QLineEdit, using a QHBoxLayout. Then align the QLabel and the QLineEdit
    vertically with the checkboxes associated with the simple search,
    using a QVBoxLayout. Create also a QVBoxLayout for the buttons.
    Finally, lay out the two latter layouts and the extension widget
    using a QGridLayout.

    \snippet dialogs/extension/finddialog.cpp 5

    Hide the extension widget using the QWidget::hide()
    function, making the application only show the simple search
    options when it starts. When the user wants to access the advanced
    search options, the dialog only needs to change the visibility of
    the extension widget. Qt's layout management takes care of the
    dialog's appearance.
*/