[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/layout/layout.doc:4 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Layout Managers</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Layout Managers</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | This example shows simple and intermediate use of Qt's layout
|
---|
| 37 | classes, <a href="qgridlayout.html">QGridLayout</a>, <a href="qboxlayout.html">QBoxLayout</a> etc.
|
---|
| 38 | <p> <hr>
|
---|
| 39 | <p> Implementation:
|
---|
| 40 | <p> <pre>/****************************************************************************
|
---|
| 41 | ** $Id: layout-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 42 | **
|
---|
| 43 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 44 | **
|
---|
| 45 | ** This file is part of an example program for Qt. This example
|
---|
| 46 | ** program may be used, distributed and modified without limitation.
|
---|
| 47 | **
|
---|
| 48 | *****************************************************************************/
|
---|
| 49 |
|
---|
| 50 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 51 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
| 52 | #include <<a href="qcolor-h.html">qcolor.h</a>>
|
---|
| 53 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
| 54 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
| 55 | #include <<a href="qlineedit-h.html">qlineedit.h</a>>
|
---|
| 56 | #include <<a href="qmultilineedit-h.html">qmultilineedit.h</a>>
|
---|
| 57 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
| 58 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
| 59 |
|
---|
| 60 | class ExampleWidget : public <a href="qwidget.html">QWidget</a>
|
---|
| 61 | {
|
---|
| 62 | public:
|
---|
| 63 | ExampleWidget( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
|
---|
| 64 | ~ExampleWidget();
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | <a name="f260"></a>ExampleWidget::ExampleWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 68 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
| 69 | {
|
---|
| 70 | // Make the top-level layout; a vertical box to contain all widgets
|
---|
| 71 | // and sub-layouts.
|
---|
| 72 | <a href="qboxlayout.html">QBoxLayout</a> *topLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 5 );
|
---|
| 73 |
|
---|
| 74 | // Create a menubar...
|
---|
| 75 | <a href="qmenubar.html">QMenuBar</a> *menubar = new <a href="qmenubar.html">QMenuBar</a>( this );
|
---|
| 76 | <a name="x540"></a> menubar-><a href="qmenubar.html#setSeparator">setSeparator</a>( QMenuBar::InWindowsStyle );
|
---|
| 77 | <a href="qpopupmenu.html">QPopupMenu</a>* popup;
|
---|
| 78 | popup = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
| 79 | popup-><a href="qmenudata.html#insertItem">insertItem</a>( "&Quit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 80 | menubar-><a href="qmenudata.html#insertItem">insertItem</a>( "&File", popup );
|
---|
| 81 |
|
---|
| 82 | // ...and tell the layout about it.
|
---|
| 83 | <a name="x539"></a> topLayout-><a href="qlayout.html#setMenuBar">setMenuBar</a>( menubar );
|
---|
| 84 |
|
---|
| 85 | // Make an hbox that will hold a row of buttons.
|
---|
| 86 | <a href="qboxlayout.html">QBoxLayout</a> *buttons = new <a href="qhboxlayout.html">QHBoxLayout</a>( topLayout );
|
---|
| 87 | int i;
|
---|
| 88 | for ( i = 1; i <= 4; i++ ) {
|
---|
| 89 | <a href="qpushbutton.html">QPushButton</a>* but = new <a href="qpushbutton.html">QPushButton</a>( this );
|
---|
| 90 | <a href="qstring.html">QString</a> s;
|
---|
| 91 | <a name="x542"></a> s.<a href="qstring.html#sprintf">sprintf</a>( "Button %d", i );
|
---|
| 92 | <a name="x530"></a> but-><a href="qbutton.html#setText">setText</a>( s );
|
---|
| 93 |
|
---|
| 94 | // Set horizontal <a href="layout.html#stretch-factor">stretch factor</a> to 10 to let the buttons
|
---|
| 95 | // stretch horizontally. The buttons will not stretch
|
---|
| 96 | // vertically, since bigWidget below will take up vertical
|
---|
| 97 | // stretch.
|
---|
| 98 | <a name="x529"></a> buttons-><a href="qboxlayout.html#addWidget">addWidget</a>( but, 10 );
|
---|
| 99 | // (Actually, the result would have been the same with a
|
---|
| 100 | // stretch factor of 0; if no items in a layout have non-zero
|
---|
| 101 | // stretch, the space is divided equally between members.)
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | // Make another hbox that will hold a left-justified row of buttons.
|
---|
| 105 | <a href="qboxlayout.html">QBoxLayout</a> *buttons2 = new <a href="qhboxlayout.html">QHBoxLayout</a>( topLayout );
|
---|
| 106 |
|
---|
| 107 | <a href="qpushbutton.html">QPushButton</a>* but = new <a href="qpushbutton.html">QPushButton</a>( "Button five", this );
|
---|
| 108 | buttons2-><a href="qboxlayout.html#addWidget">addWidget</a>( but );
|
---|
| 109 |
|
---|
| 110 | but = new <a href="qpushbutton.html">QPushButton</a>( "Button 6", this );
|
---|
| 111 | buttons2-><a href="qboxlayout.html#addWidget">addWidget</a>( but );
|
---|
| 112 |
|
---|
| 113 | // Fill up the rest of the hbox with stretchable space, so that
|
---|
| 114 | // the buttons get their minimum width and are pushed to the left.
|
---|
| 115 | <a name="x528"></a> buttons2-><a href="qboxlayout.html#addStretch">addStretch</a>( 10 );
|
---|
| 116 |
|
---|
| 117 | // Make a big widget that will grab all space in the middle.
|
---|
| 118 | <a href="qmultilineedit.html">QMultiLineEdit</a> *bigWidget = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this );
|
---|
| 119 | bigWidget-><a href="qtextedit.html#setText">setText</a>( "This widget will get all the remaining space" );
|
---|
| 120 | <a name="x531"></a> bigWidget-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Plain );
|
---|
| 121 |
|
---|
| 122 | // Set vertical stretch factor to 10 to let the bigWidget stretch
|
---|
| 123 | // vertically. It will stretch horizontally because there are no
|
---|
| 124 | // widgets beside it to take up horizontal stretch.
|
---|
| 125 | // topLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( bigWidget, 10 );
|
---|
| 126 | topLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( bigWidget );
|
---|
| 127 |
|
---|
| 128 | // Make a grid that will hold a vertical table of QLabel/QLineEdit
|
---|
| 129 | // pairs next to a large QMultiLineEdit.
|
---|
| 130 |
|
---|
| 131 | // Don't use hard-coded row/column numbers in QGridLayout, you'll
|
---|
| 132 | // regret it when you have to change the layout.
|
---|
| 133 | const int numRows = 3;
|
---|
| 134 | const int labelCol = 0;
|
---|
| 135 | const int linedCol = 1;
|
---|
| 136 | const int multiCol = 2;
|
---|
| 137 |
|
---|
| 138 | // Let the grid-layout have a spacing of 10 pixels between
|
---|
| 139 | // widgets, overriding the default from topLayout.
|
---|
| 140 | <a href="qgridlayout.html">QGridLayout</a> *grid = new <a href="qgridlayout.html">QGridLayout</a>( topLayout, 0, 0, 10 );
|
---|
| 141 | int row;
|
---|
| 142 |
|
---|
| 143 | for ( row = 0; row < numRows; row++ ) {
|
---|
| 144 | <a href="qlineedit.html">QLineEdit</a> *ed = new <a href="qlineedit.html">QLineEdit</a>( this );
|
---|
| 145 | // The line edit goes in the second column
|
---|
| 146 | <a name="x533"></a> grid-><a href="qgridlayout.html#addWidget">addWidget</a>( ed, row, linedCol );
|
---|
| 147 |
|
---|
| 148 | // Make a label that is a buddy of the line edit
|
---|
| 149 | <a href="qstring.html">QString</a> s;
|
---|
| 150 | s.<a href="qstring.html#sprintf">sprintf</a>( "Line &%d", row+1 );
|
---|
| 151 | <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>( ed, s, this );
|
---|
| 152 | // The label goes in the first column.
|
---|
| 153 | grid-><a href="qgridlayout.html#addWidget">addWidget</a>( label, row, labelCol );
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // The multiline edit will cover the entire vertical range of the
|
---|
| 157 | // grid (rows 0 to numRows) and stay in column 2.
|
---|
| 158 |
|
---|
| 159 | <a href="qmultilineedit.html">QMultiLineEdit</a> *med = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this );
|
---|
| 160 | <a name="x532"></a> grid-><a href="qgridlayout.html#addMultiCellWidget">addMultiCellWidget</a>( med, 0, -1, multiCol, multiCol );
|
---|
| 161 |
|
---|
| 162 | // The labels will take the space they need. Let the remaining
|
---|
| 163 | // horizontal space be shared so that the multiline edit gets
|
---|
| 164 | // twice as much as the line edit.
|
---|
| 165 | <a name="x534"></a> grid-><a href="qgridlayout.html#setColStretch">setColStretch</a>( linedCol, 10 );
|
---|
| 166 | grid-><a href="qgridlayout.html#setColStretch">setColStretch</a>( multiCol, 20 );
|
---|
| 167 |
|
---|
| 168 | // Add a widget at the bottom.
|
---|
| 169 | <a href="qlabel.html">QLabel</a>* sb = new <a href="qlabel.html">QLabel</a>( this );
|
---|
| 170 | sb-><a href="qlabel.html#setText">setText</a>( "Let's pretend this is a status bar" );
|
---|
| 171 | sb-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
|
---|
| 172 | // This widget will use all horizontal space, and have a fixed height.
|
---|
| 173 |
|
---|
| 174 | // we should have made a subclass and implemented sizePolicy there...
|
---|
| 175 | <a name="x545"></a><a name="x537"></a> sb-><a href="qwidget.html#setFixedHeight">setFixedHeight</a>( sb-><a href="qwidget.html#sizeHint">sizeHint</a>().height() );
|
---|
| 176 |
|
---|
| 177 | <a name="x535"></a> sb-><a href="qlabel.html#setAlignment">setAlignment</a>( AlignVCenter | AlignLeft );
|
---|
| 178 | topLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( sb );
|
---|
| 179 |
|
---|
| 180 | <a name="x538"></a> topLayout-><a href="qlayout.html#activate">activate</a>();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | ExampleWidget::~ExampleWidget()
|
---|
| 184 | {
|
---|
| 185 | // All child widgets are deleted by Qt.
|
---|
| 186 | // The top-level layout and all its sub-layouts are deleted by Qt.
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | int main( int argc, char **argv )
|
---|
| 190 | {
|
---|
| 191 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 192 |
|
---|
| 193 | ExampleWidget f;
|
---|
| 194 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(&f);
|
---|
| 195 | f.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Layouts");
|
---|
| 196 | f.<a href="qwidget.html#show">show</a>();
|
---|
| 197 |
|
---|
| 198 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 199 | }
|
---|
| 200 | </pre>
|
---|
| 201 |
|
---|
| 202 | <p>See also <a href="examples.html">Examples</a>.
|
---|
| 203 |
|
---|
| 204 | <!-- eof -->
|
---|
| 205 | <p><address><hr><div align=center>
|
---|
| 206 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 207 | <td>Copyright © 2007
|
---|
| 208 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 209 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 210 | </table></div></address></body>
|
---|
| 211 | </html>
|
---|