~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/layouts-basiclayouts.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/basiclayouts.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Basic Layouts Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Basic Layouts Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="layouts-basiclayouts-dialog-cpp.html">layouts/basiclayouts/dialog.cpp</a></li>
 
24
<li><a href="layouts-basiclayouts-dialog-h.html">layouts/basiclayouts/dialog.h</a></li>
 
25
<li><a href="layouts-basiclayouts-main-cpp.html">layouts/basiclayouts/main.cpp</a></li>
 
26
</ul>
 
27
<p>The Basic Layouts example shows how to use the standard layout managers that are available in Qt: <a href="qboxlayout.html">QBoxLayout</a> and <a href="qgridlayout.html">QGridLayout</a>.</p>
 
28
<center><img src="images/basiclayouts-example.png" alt="Screenshot of the Basic Layouts example" /></center><p>The <a href="qboxlayout.html">QBoxLayout</a> class lines up widgets horizontally or vertically. <a href="qhboxlayout.html">QHBoxLayout</a> and <a href="qvboxlayout.html">QVBoxLayout</a> are convenience subclasses of <a href="qboxlayout.html">QBoxLayout</a>.</p>
 
29
<a name="dialog-class-definition"></a>
 
30
<h2>Dialog Class Definition</h2>
 
31
<pre>&nbsp;   class Dialog : public QDialog
 
32
    {
 
33
        Q_OBJECT
 
34
 
 
35
    public:
 
36
        Dialog();
 
37
 
 
38
    private:
 
39
        void createMenu();
 
40
        void createHorizontalGroupBox();
 
41
        void createGridGroupBox();
 
42
 
 
43
        enum { NumGridRows = 3, NumButtons = 4 };
 
44
 
 
45
        QMenuBar *menuBar;
 
46
        QGroupBox *horizontalGroupBox;
 
47
        QGroupBox *gridGroupBox;
 
48
        QTextEdit *smallEditor;
 
49
        QTextEdit *bigEditor;
 
50
        QLabel *labels[NumGridRows];
 
51
        QLineEdit *lineEdits[NumGridRows];
 
52
        QPushButton *buttons[NumButtons];
 
53
        QPushButton *okButton;
 
54
        QPushButton *cancelButton;
 
55
 
 
56
        QMenu *fileMenu;
 
57
        QAction *exitAction;
 
58
    };</pre>
 
59
<p>The <tt>Dialog</tt> class inherits <a href="qdialog.html">QDialog</a>. It is a custom widget that displays its child widgets using the geometry managers: <a href="qhboxlayout.html">QHBoxLayout</a>, <a href="qvboxlayout.html">QVBoxLayout</a> and <a href="qgridlayout.html">QGridLayout</a>.</p>
 
60
<p>We declare three private functions to simplify the class constructor: The <tt>createMenu()</tt>, <tt>createHorizontalGroupBox()</tt> and <tt>createGridGroupBox()</tt> functions create several widgets that the example uses to demonstrate how the layout affects their appearances.</p>
 
61
<a name="dialog-class-implementation"></a>
 
62
<h2>Dialog Class Implementation</h2>
 
63
<pre>&nbsp;   Dialog::Dialog()
 
64
    {
 
65
        createMenu();
 
66
        createHorizontalGroupBox();
 
67
        createGridGroupBox();
 
68
 
 
69
        bigEditor = new QTextEdit;
 
70
        bigEditor-&gt;setPlainText(tr(&quot;This widget takes up all the remaining space &quot;
 
71
                                   &quot;in the top-level layout.&quot;));
 
72
 
 
73
        okButton = new QPushButton(tr(&quot;OK&quot;));
 
74
        cancelButton = new QPushButton(tr(&quot;Cancel&quot;));
 
75
        okButton-&gt;setDefault(true);
 
76
 
 
77
        connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
 
78
        connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));</pre>
 
79
<p>In the constructor, we first use the <tt>createMenu()</tt> function to create and populate a menu bar, the private <tt>createHorizontalGroupBox()</tt> function to create a group box containing four buttons with a horizontal layout, and the private <tt>createGridGroupBox()</tt> function to create a group box containing several line edits and a small text editor, displayed in a grid layout.</p>
 
80
<p>The menu bar and group boxes are in turn managed by the <tt>Dialog</tt> widget's main layout. This outer layout also manages several other widgets: We create a big text editor and a couple of buttons.</p>
 
81
<p>Note that we don't have to specify a parent for the widgets when we create them. The reason is that all the widgets we create here will be added to a layout, and when we add a widget to a layout, it is automatically reparented to the widget the layout is installed on.</p>
 
82
<pre>&nbsp;       QHBoxLayout *buttonLayout = new QHBoxLayout;</pre>
 
83
<p>Before we create and customize the main layout, we create a layout for the <b>OK</b> and <b>Cancel</b> buttons. We use a <a href="qhboxlayout.html">QHBoxLayout</a> for this purpose.</p>
 
84
<p>The <a href="qboxlayout.html">QBoxLayout</a> class takes in general the space it gets (from its parent layout or from the parent widget), divides it up into a series of boxes, and makes each managed widget fill one box. If the <a href="qboxlayout.html">QBoxLayout</a>'s orientation is <a href="qt.html#Orientation-enum">Qt::Horizontal</a> the boxes are placed in a row. If the orientation is <a href="qt.html#Orientation-enum">Qt::Vertical</a>, the boxes are placed in a column. The corresponding convenience classes are <a href="qhboxlayout.html">QHBoxLayout</a> and <a href="qvboxlayout.html">QVBoxLayout</a>, respectively.</p>
 
85
<pre>&nbsp;       buttonLayout-&gt;addStretch(1);</pre>
 
86
<p>In addition to adding the two buttons to the layout, we also add a stretchable space to the layout using the <a href="qboxlayout.html#addStretch">QBoxLayout::addStretch</a>() function. The layout will display its widgets and its stretchable spaces according to their <a href="qwidget.html#sizePolicy-prop">size policy</a>, <a href="qboxlayout.html#addWidget">strech factors</a> and <a href="qwidget.html#sizeHint-prop">size hints</a>. Since we add the stretch before we add the widgets, the buttons will be located in the application's right corner.</p>
 
87
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
88
<tr valign="top" bgcolor="#f0f0f0"><td><img src="images/basiclayouts_double_stretch.png" alt="Double stretch" /></td><td><img src="images/basiclayouts_no_stretch.png" alt="No stretch" /></td></tr>
 
89
</table>
 
90
<p>If we also added another stretch after adding the buttons, the buttons would be centered as shown in the left screenshot above. The right screenshot shows how the buttons would appear if we didn't add any stretchable space at all: The buttons would then stretch to fill the avaliable space.</p>
 
91
<pre>&nbsp;       buttonLayout-&gt;addWidget(okButton);
 
92
        buttonLayout-&gt;addWidget(cancelButton);</pre>
 
93
<p>We use the <a href="qboxlayout.html#addWidget">QBoxLayout::addWidget</a>() function to add the widgets to the end of layout. Each widget will get at least its minimum size and at most its maximum size. It is possible to specify a stretch factor in the <a href="qboxlayout.html#addWidget">addWidget()</a> function, and any excess space is shared according to these stretch factors. If not specified, a widget's stretch factor is 0.</p>
 
94
<pre>&nbsp;       QVBoxLayout *mainLayout = new QVBoxLayout;
 
95
        mainLayout-&gt;setMenuBar(menuBar);
 
96
        mainLayout-&gt;addWidget(horizontalGroupBox);
 
97
        mainLayout-&gt;addWidget(gridGroupBox);
 
98
        mainLayout-&gt;addWidget(bigEditor);
 
99
        mainLayout-&gt;addLayout(buttonLayout);
 
100
        setLayout(mainLayout);
 
101
 
 
102
        setWindowTitle(tr(&quot;Basic Layouts&quot;));
 
103
    }</pre>
 
104
<p>The outer layout for the <tt>Dialog</tt> widget is a <a href="qvboxlayout.html">QVBoxLayout</a>.</p>
 
105
<p>When we call the <a href="qlayout.html#setMenuBar">QLayout::setMenuBar</a>() function, the layout places the provided menu bar at the top of the parent widget, and outside the widget's <a href="qwidget.html#contentsRect">content margins</a>. All child widgets are placed below the bottom edge of the menu bar.</p>
 
106
<p>In the same way we use <a href="qboxlayout.html#addWidget">QBoxLayout::addWidget</a>(), we can use <a href="qboxlayout.html#addLayout">QBoxLayout::addLayout</a>() to add another layout, and its widgets, to the end of the main layout. Again it is possible to specify a stretch factor which, if not specified, is 0.</p>
 
107
<p>We install the main layout on the <tt>Dialog</tt> widget using the <a href="qwidget.html#setLayout">QWidget::setLayout</a>() function, and all of the layout's widgets are automatically reparented to be children of the <tt>Dialog</tt> widget.</p>
 
108
<pre>&nbsp;   void Dialog::createMenu()
 
109
    {
 
110
        menuBar = new QMenuBar;
 
111
 
 
112
        fileMenu = new QMenu(tr(&quot;&amp;File&quot;), this);
 
113
        exitAction = fileMenu-&gt;addAction(tr(&quot;E&amp;xit&quot;));
 
114
        menuBar-&gt;addMenu(fileMenu);
 
115
 
 
116
        connect(exitAction, SIGNAL(triggered()), this, SLOT(accept()));
 
117
    }</pre>
 
118
<p>In the private <tt>createMenu()</tt> function we create a menu bar, and add a pull-down <b>File</b> menu containing an <b>Exit</b> option.</p>
 
119
<pre>&nbsp;   void Dialog::createHorizontalGroupBox()
 
120
    {
 
121
        horizontalGroupBox = new QGroupBox(tr(&quot;Horizontal layout&quot;));
 
122
        QHBoxLayout *layout = new QHBoxLayout;
 
123
 
 
124
        for (int i = 0; i &lt; NumButtons; ++i) {
 
125
            buttons[i] = new QPushButton(tr(&quot;Button %1&quot;).arg(i + 1));
 
126
            layout-&gt;addWidget(buttons[i]);
 
127
        }
 
128
        horizontalGroupBox-&gt;setLayout(layout);
 
129
    }</pre>
 
130
<p>When we create the horizontal group box, we use a <a href="qhboxlayout.html">QHBoxLayout</a> as the internal layout. We create the buttons we want to put in the group box, add them to the layout and install the layout on the group box.</p>
 
131
<pre>&nbsp;   void Dialog::createGridGroupBox()
 
132
    {
 
133
        gridGroupBox = new QGroupBox(tr(&quot;Grid layout&quot;));</pre>
 
134
<p>In the <tt>createGridGroupBox()</tt> function we use a <a href="qgridlayout.html">QGridLayout</a> which lays out widgets in a grid. It takes the space made available to it (by its parent layout or by the parent widget), divides it up into rows and columns, and puts each widget it manages into the correct cell.</p>
 
135
<pre>&nbsp;       for (int i = 0; i &lt; NumGridRows; ++i) {
 
136
            labels[i] = new QLabel(tr(&quot;Line %1:&quot;).arg(i + 1));
 
137
            lineEdits[i] = new QLineEdit;
 
138
            layout-&gt;addWidget(labels[i], i, 0);
 
139
            layout-&gt;addWidget(lineEdits[i], i, 1);
 
140
        }</pre>
 
141
<p>For each row in the grid we create a label and an associated line edit, and add them to the layout. The <a href="qgridlayout.html#addWidget">QGridLayout::addWidget</a>() function differ from the corresponding function in <a href="qboxlayout.html">QBoxLayout</a>: It needs the row and column specifying the grid cell to put the widget in.</p>
 
142
<pre>&nbsp;       smallEditor = new QTextEdit;
 
143
        smallEditor-&gt;setPlainText(tr(&quot;This widget takes up about two thirds of the &quot;
 
144
                                     &quot;grid layout.&quot;));
 
145
        layout-&gt;addWidget(smallEditor, 0, 2, 3, 1);</pre>
 
146
<p><a href="qgridlayout.html#addWidget">QGridLayout::addWidget</a>() can in addition take arguments specifying the number of rows and columns the cell will be spanning. In this example, we create a small editor which spans three rows and one column.</p>
 
147
<p>For both the <a href="qboxlayout.html#addWidget">QBoxLayout::addWidget</a>() and <a href="qgridlayout.html#addWidget">QGridLayout::addWidget</a>() functions it is also possible to add a last argument specifying the widget's alignment. By default it fills the whole cell. But we could, for example, align a widget with the right edge by specifying the alignment to be <a href="qt.html#AlignmentFlag-enum">Qt::AlignRight</a>.</p>
 
148
<pre>&nbsp;       layout-&gt;setColumnStretch(1, 10);
 
149
        layout-&gt;setColumnStretch(2, 20);
 
150
        gridGroupBox-&gt;setLayout(layout);
 
151
    }</pre>
 
152
<p>Each column in a grid layout has a stretch factor. The stretch factor is set using <a href="qgridlayout.html#setColumnStretch">QGridLayout::setColumnStretch</a>() and determines how much of the available space the column will get over and above its necessary minimum.</p>
 
153
<p>In this example, we set the stretch factors for columns 1 and 2. The stretch factor is relative to the other columns in this grid; columns with a higher stretch factor take more of the available space. So column 2 in our grid layout will get more of the available space than column 1, and column 0 will not grow at all since its stretch factor is 0 (the default).</p>
 
154
<p>Columns and rows behave identically; there is an equivalent stretch factor for rows, as well as a <a href="qgridlayout.html#setRowStretch">QGridLayout::setRowStretch</a>() function.</p>
 
155
<p /><address><hr /><div align="center">
 
156
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
157
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
158
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
159
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
160
</tr></table></div></address></body>
 
161
</html>