summaryrefslogtreecommitdiffstats
path: root/doc/src/examples/basicgraphicslayouts.qdoc
blob: e0af2e88d0c3030f555efe49c26571f9dab815e1 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example graphicsview/basicgraphicslayouts
    \title Basic Graphics Layouts Example

    The Basic Graphics Layouts example shows how to use the layout classes
    in QGraphicsView: QGraphicsLinearLayout and QGraphicsGridLayout.

    \image basicgraphicslayouts-example.png Screenshot of the Basic Layouts Example

    \section1 Window Class Definition

    The \c Window class is a subclass of QGraphicsWidget. It has a
    constructor with a QGraphicsWidget \a parent as its parameter.

    \snippet examples/graphicsview/basicgraphicslayouts/window.h 0

    \section1 Window Class Implementation

    The constructor of \c Window instantiates a QGraphicsLinearLayout object,
    \c windowLayout, with vertical orientation. We instantiate another
    QGraphicsLinearLayout object, \c linear, whose parent is \c windowLayout.
    Next, we create a \c LayoutItem object, \c item and add it to \c linear
    with the \l{QGraphicsLinearLayout::}{addItem()} function. We also provide
    \c item with a \l{QGraphicsLinearLayout::setStretchFactor()}
    {stretchFactor}.

    \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 0

    We repeat the process:

    \list
        \o create a new \c LayoutItem,
        \o add the item \c linear, and
        \o provide a stretch factor.
    \endlist

    \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 1

    We then add \c linear to \c windowLayout, nesting two
    QGraphicsLinearLayout objects. Apart from the QGraphicsLinearLayout, we
    also use a QGraphicsGridLayout object, \c grid, which is a 4x3 grid with
    some cells spanning to other rows.

    We create seven \c LayoutItem objects and place them into \c grid with
    the \l{QGraphicsGridLayout::}{addItem()} function as shown in the code
    snippet below:

    \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 2

    The first item we add to \c grid is placed in the top left cell,
    spanning four rows. The next two items are placed in the second column,
    and they span two rows. Each item's \l{QGraphicsWidget::}{maximumHeight()}
    and \l{QGraphicsWidget::}{minimumHeight()} are set to be equal so that
    they do not expand vertically. As a result, these items will not
    fit vertically in their cells. So, we specify that they should be
    vertically aligned in the center of the cell using Qt::AlignVCenter.

    Finally, \c grid itself is added to \c windowLayout. Unlike
    QGridLayout::addItem(), QGraphicsGridLayout::addItem() requires a row
    and a column for its argument, specifying which cell the item should be
    positioned in. Also, if the \c rowSpan and \c columnSpan arguments
    are omitted, they will default to 1.

    Note that we do not specify a parent for each \c LayoutItem that we
    construct, as all these items will be added to \c windowLayout. When we
    add an item to a layout, it will be automatically reparented to the widget
    on which the layout is installed.

    \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 3

    Now that we have set up \c grid and added it to \c windowLayout, we
    install \c windowLayout onto the window object using
    QGraphicsWidget::setLayout() and we set the window title.

    \section1 LayoutItem Class Definition

    The \c LayoutItem class is a subclass of QGraphicsWidget. It has a
    constructor, a destructor, and a reimplementation of the
    {QGraphicsItem::paint()}{paint()} function.

    \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.h 0

    The \c LayoutItem class also has a private instance of QPixmap, \c pix.

    \note We subclass QGraphicsWidget so that \c LayoutItem objects can
    be automatically plugged into a layout, as QGraphicsWidget is a
    specialization of QGraphicsLayoutItem.

    \section1 LayoutItem Class Implementation

    In \c{LayoutItem}'s constructor, \c pix is instantiated and the
    \c{QT_original_R.png} image is loaded into it. We set the size of
    \c LayoutItem to be slightly larger than the size of the pixmap as we
    require some space around it for borders that we will paint later.
    Alternatively, you could scale the pixmap to prevent the item from
    becoming smaller than the pixmap.

    \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 0

    We use the Q_UNUSED() macro to prevent the compiler from generating
    warnings regarding unused parameters.

    \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 1

    The idea behind the \c paint() function is to paint the
    background rect then paint a rect around the pixmap.

    \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 2

*/