📄 layout.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - Layout Classes</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Layout Classes</h1><br clear="all">The Qt layout system provides a simple and powerful way of specifyingthe layout of child widgets.<p>By specifying the logical layout once, you get the following benefits:<ul><li> Positioning of child widgets.<li> Sensible default sizes for top-level widgets.<li> Sensible minimum sizes for top-level widgets.<li> Resize handling.<li> Automatic update when contents change: <ul> <li> Font size, text or other contents of subwidgets. <li> Hiding or showing a subwidget. <li> Removal of subwidget. </ul></ul><p><h2>Layout Widgets</h2><p>The easiest way to give your widgets a good layout is to use thelayout widgets: <a href="qhbox.html">QHBox</a>, <a href="qvbox.html">QVBox</a> and <a href="qgrid.html">QGrid</a>. A layout widget automaticallylays out its children in the order they are constructed. To make morecomplex layouts, you can nest layout widgets inside each other.<p><dl> <dt><a href="qhbox.html">QHBox</a> <dd>A QHBox lays out its children in a horizontal row, left to right. <br><img src="qhbox-m.png" alt="QHBox with five children."><p> <dt><a href="qvbox.html">QVBox</a> <dd>A QVBox lays out its children in a vertical row, top to bottom. <br><img src="qvbox-m.png" alt="QVBox with five children."> <dt><a href="qgrid.html">QGrid</a> <dd>A QGrid lays out its children in a table. You specify how many columns the table has, and it is filled left to right, beginning a new row when the previous is filled up. The grid is fixed; the children will not flow to other rows as the widget is resized. <br><img src="qgrid-m.png" alt="Two-column QGrid with five children."></dl><p>The grid above can be produced by the following code:<pre> <a href="qgrid.html">QGrid</a> *main = new <a href="qgrid.html">QGrid</a>( 2 ); // a 2 x n grid new QLabel( "One", main ); new QLabel( "Two", main ); new QLabel( "Three", main ); new QLabel( "Four", main ); new QLabel( "Five", main );</pre><p>You can adjust the layout somewhat by calling <a href="qwidget.html#c0b5fb">setMinimumSize()</a> or<a href="qwidget.html#87e3f4">setFixedSize()</a> onthe children.<p><h2>QLayout</h2><p>If you need more control over the layout, use a <a href="qlayout.html">QLayout</a> subclass. The layout classes included in Qt 2.1 are <a href="qgridlayout.html">QGridLayout</a> and <a href="qboxlayout.html">QBoxLayout</a>.(<a href="qhboxlayout.html">QHBoxLayout</a> and<a href="qvboxlayout.html">QVBoxLayout</a> are trivial subclasses of QBoxLayout,that are simpler to use and make the code easier to read.)<p>When you use a layout, you have to insert each child both into itsparent widget (done in the constructor) and into its layout (typicallydone with a function called <code>addWidget()).</code> This way, you can givelayout parameters for each widget, specifying properties likealignment, stretch, and placement.<p>The following code makes a grid like the one above, with a couple ofimprovements:<pre> <a href="qwidget.html">QWidget</a> *main = new <a href="qwidget.html">QWidget</a>; //make a 1x1 grid, it will auto-expand: <a href="qgridlayout.html">QGridLayout</a> *grid = new <a href="qgridlayout.html">QGridLayout</a>( main, 1, 1 ); //add the first four widgets with (row, column) addressing grid-><a href="qgridlayout.html#dac29c">addWidget</a>( new QLabel( "One", main ), 0, 0 ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( new QLabel( "Two", main ), 0, 1 ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( new QLabel( "Three", main ), 1, 0 ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( new QLabel( "Four", main ), 1, 1 ); //add the last widget on row 2, let it span from column 0 to //column 1, and let it be aligned center. grid->addMultiCellWidget( new QLabel( "Five", main ), 2, 2, 0, 1, Qt::AlignCenter ); //let the ratio between the widths of columns 0 and 1 be 2:3. grid->setColStretch( 0, 2 ); grid-><a href="qgridlayout.html#df80c4">setColStretch</a>( 1, 3 );</pre><p><p>You can insert layouts inside a layout by giving the parent layout asa parameter in the constructor.<pre> <a href="qwidget.html">QWidget</a>* main = new <a href="qwidget.html">QWidget</a>; <a href="qlineedit.html">QLineEdit</a>* field = new <a href="qlineedit.html">QLineEdit</a>( main ); <a href="qpushbutton.html">QPushButton</a>* ok = new <a href="qpushbutton.html">QPushButton</a>( "OK", main ); <a href="qpushbutton.html">QPushButton</a>* cancel = new <a href="qpushbutton.html">QPushButton</a>( "Cancel", main ); <a href="qlabel.html">QLabel</a>* label = new <a href="qlabel.html">QLabel</a>( "Where do you want to go?", main ); <a href="qvboxlayout.html">QVBoxLayout</a> *vbox = new <a href="qvboxlayout.html">QVBoxLayout</a>( main ); // A layout on a widget vbox-><a href="qboxlayout.html#ebba99">addWidget</a>( label ); vbox-><a href="qboxlayout.html#ebba99">addWidget</a>( field ); <a href="qhboxlayout.html">QHBoxLayout</a> *buttons = new <a href="qhboxlayout.html">QHBoxLayout</a>( vbox ); // A layout inside a layout buttons-><a href="qboxlayout.html#ebba99">addWidget</a>( ok ); buttons-><a href="qboxlayout.html#ebba99">addWidget</a>( cancel );</pre><p>If you are not satisfied with thedefault placement, you can create the layout without a parent andthen insert it with <code>addLayout().</code><p><h2>Custom Layouts</h2><p>If the built-in layout classes are not sufficient, you can define yourown. You will have to make a subclass of <a href="qlayout.html">QLayout</a>that handles resizing and size calculations, as well as a subclass of<a href="qglayoutiterator.html">QGLayoutIterator</a> to iterate over yourlayout class.<p>See the <a href="customlayout.html">Custom Layout</a> page for anin-depth description.<p><h2>Custom Widgets In Layouts</h2><p>When you make your own widget class, you should also communicate itslayout properties. If the widget has a QLayout, this is already takencare of. If the widget does not have any children, or uses manuallayout, you should reimplement the following QWidget member functions:<p><dl> <dt><a href="qwidget.html#4511d1">sizeHint()</a> <dd> Returns the preferred size of the widget. <dt><a href="qwidget.html#553e08">minimumSizeHint()</a> <dd> Returns the smallest size the widget can have. <dt><a href="qwidget.html#23726d">sizePolicy()</a> <dd> Returns a <a href="qsizepolicy.html">QSizePolicy</a>; a value describing the space requirements of the widget.<p></dl><p>Call <a href="qwidget.html#5f7c35">updateGeometry()</a> wheneverthe size hint, minimum size hint or size policy changes. This willcause a layout recalculation. Multiple calls to updateGeometry() willonly cause one recalculation.<p>If the preferred height of your widget depends on its actual width(eg. a label with automatic word-breaking), set the <code>hasHeightForWidth()</code>flag in <code>sizePolicy(),</code> and reimplement <a href="qwidget.html#1a2d58">heightForWidth()</a>.<p>Even if you implement heightForWidth(), it is still necessary toprovide a good sizeHint(). The sizeHint() provides the preferred widthof the widget, and it is used by QLayout subclasses that do notsupport heightForWidth() (both QGridLayout and QBoxLayout support it).<p>For further guidance when implementing these functions, see theirimplementations in existing Qt classes that have similar layoutrequirements to your new widget.<p><h2>Manual Layout</h2><p>If you are making a one-of-a-kind special layout, you can also make acustom widget as described above. Reimplement <a href="qwidget.html#7d375f">resizeEvent()</a> to calculate therequired distribution of sizes and call <a href="qwidget.html#9ede68">setGeometry()</a> on each child.<p>The widget will get an event with <a href="qevent.html#4d3e5b">type</a><code>LayoutHint</code> when the layout needs to berecalculated. Reimplement <a href="qwidget.html#6ff658">event()</a>to be notified of <code>LayoutHint</code> events.<p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -