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 graphicsview/basicgraphicslayouts
|
---|
44 | \title Basic Graphics Layouts Example
|
---|
45 |
|
---|
46 | The Basic Graphics Layouts example shows how to use the layout classes
|
---|
47 | in QGraphicsView: QGraphicsLinearLayout and QGraphicsGridLayout.
|
---|
48 |
|
---|
49 | \image basicgraphicslayouts-example.png Screenshot of the Basic Layouts Example
|
---|
50 |
|
---|
51 | \section1 Window Class Definition
|
---|
52 |
|
---|
53 | The \c Window class is a subclass of QGraphicsWidget. It has a
|
---|
54 | constructor with a QGraphicsWidget \a parent as its parameter.
|
---|
55 |
|
---|
56 | \snippet examples/graphicsview/basicgraphicslayouts/window.h 0
|
---|
57 |
|
---|
58 | \section1 Window Class Implementation
|
---|
59 |
|
---|
60 | The constructor of \c Window instantiates a QGraphicsLinearLayout object,
|
---|
61 | \c windowLayout, with vertical orientation. We instantiate another
|
---|
62 | QGraphicsLinearLayout object, \c linear, whose parent is \c windowLayout.
|
---|
63 | Next, we create a \c LayoutItem object, \c item and add it to \c linear
|
---|
64 | with the \l{QGraphicsLinearLayout::}{addItem()} function. We also provide
|
---|
65 | \c item with a \l{QGraphicsLinearLayout::setStretchFactor()}
|
---|
66 | {stretchFactor}.
|
---|
67 |
|
---|
68 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 0
|
---|
69 |
|
---|
70 | We repeat the process:
|
---|
71 |
|
---|
72 | \list
|
---|
73 | \o create a new \c LayoutItem,
|
---|
74 | \o add the item \c linear, and
|
---|
75 | \o provide a stretch factor.
|
---|
76 | \endlist
|
---|
77 |
|
---|
78 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 1
|
---|
79 |
|
---|
80 | We then add \c linear to \c windowLayout, nesting two
|
---|
81 | QGraphicsLinearLayout objects. Apart from the QGraphicsLinearLayout, we
|
---|
82 | also use a QGraphicsGridLayout object, \c grid, which is a 4x3 grid with
|
---|
83 | some cells spanning to other rows.
|
---|
84 |
|
---|
85 | We create seven \c LayoutItem objects and place them into \c grid with
|
---|
86 | the \l{QGraphicsGridLayout::}{addItem()} function as shown in the code
|
---|
87 | snippet below:
|
---|
88 |
|
---|
89 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 2
|
---|
90 |
|
---|
91 | The first item we add to \c grid is placed in the top left cell,
|
---|
92 | spanning four rows. The next two items are placed in the second column,
|
---|
93 | and they span two rows. Each item's \l{QGraphicsWidget::}{maximumHeight()}
|
---|
94 | and \l{QGraphicsWidget::}{minimumHeight()} are set to be equal so that
|
---|
95 | they do not expand vertically. As a result, these items will not
|
---|
96 | fit vertically in their cells. So, we specify that they should be
|
---|
97 | vertically aligned in the center of the cell using Qt::AlignVCenter.
|
---|
98 |
|
---|
99 | Finally, \c grid itself is added to \c windowLayout. Unlike
|
---|
100 | QGridLayout::addItem(), QGraphicsGridLayout::addItem() requires a row
|
---|
101 | and a column for its argument, specifying which cell the item should be
|
---|
102 | positioned in. Also, if the \c rowSpan and \c columnSpan arguments
|
---|
103 | are omitted, they will default to 1.
|
---|
104 |
|
---|
105 | Note that we do not specify a parent for each \c LayoutItem that we
|
---|
106 | construct, as all these items will be added to \c windowLayout. When we
|
---|
107 | add an item to a layout, it will be automatically reparented to the widget
|
---|
108 | on which the layout is installed.
|
---|
109 |
|
---|
110 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 3
|
---|
111 |
|
---|
112 | Now that we have set up \c grid and added it to \c windowLayout, we
|
---|
113 | install \c windowLayout onto the window object using
|
---|
114 | QGraphicsWidget::setLayout() and we set the window title.
|
---|
115 |
|
---|
116 | \section1 LayoutItem Class Definition
|
---|
117 |
|
---|
118 | The \c LayoutItem class is a subclass of QGraphicsWidget. It has a
|
---|
119 | constructor, a destructor, and a reimplementation of the
|
---|
120 | {QGraphicsItem::paint()}{paint()} function.
|
---|
121 |
|
---|
122 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.h 0
|
---|
123 |
|
---|
124 | The \c LayoutItem class also has a private instance of QPixmap, \c pix.
|
---|
125 |
|
---|
126 | \note We subclass QGraphicsWidget so that \c LayoutItem objects can
|
---|
127 | be automatically plugged into a layout, as QGraphicsWidget is a
|
---|
128 | specialization of QGraphicsLayoutItem.
|
---|
129 |
|
---|
130 | \section1 LayoutItem Class Implementation
|
---|
131 |
|
---|
132 | In \c{LayoutItem}'s constructor, \c pix is instantiated and the
|
---|
133 | \c{QT_original_R.png} image is loaded into it. We set the size of
|
---|
134 | \c LayoutItem to be slightly larger than the size of the pixmap as we
|
---|
135 | require some space around it for borders that we will paint later.
|
---|
136 | Alternatively, you could scale the pixmap to prevent the item from
|
---|
137 | becoming smaller than the pixmap.
|
---|
138 |
|
---|
139 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 0
|
---|
140 |
|
---|
141 | We use the Q_UNUSED() macro to prevent the compiler from generating
|
---|
142 | warnings regarding unused parameters.
|
---|
143 |
|
---|
144 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 1
|
---|
145 |
|
---|
146 | The idea behind the \c paint() function is to paint the
|
---|
147 | background rect then paint a rect around the pixmap.
|
---|
148 |
|
---|
149 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 2
|
---|
150 |
|
---|
151 | */
|
---|