Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 758

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/layout/layout.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Layout Managers</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: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

   
<p> 
This example shows simple and intermediate use of Qt's layout
classes, <a href="qgridlayout.html">QGridLayout</a>, <a href="qboxlayout.html">QBoxLayout</a> etc.
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id:  qt/layout.cpp   3.0.2   edited Oct 12 12:18 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qlineedit-h.html">qlineedit.h</a>&gt;
#include &lt;<a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;

class ExampleWidget : public <a href="qwidget.html">QWidget</a>
{
public:
    ExampleWidget( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
    ~ExampleWidget();
};

<a name="f405"></a>ExampleWidget::ExampleWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qwidget.html">QWidget</a>( parent, name )
{
    // Make the top-level layout; a vertical box to contain all widgets
    // and sub-layouts.
    <a href="qboxlayout.html">QBoxLayout</a> *topLayout = new <a href="qvboxlayout.html">QVBoxLayout</a>( this, 5 );

    // Create a menubar...
    <a href="qmenubar.html">QMenuBar</a> *menubar = new <a href="qmenubar.html">QMenuBar</a>( this );
<a name="x1336"></a>    menubar-&gt;<a href="qmenubar.html#setSeparator">setSeparator</a>( QMenuBar::InWindowsStyle );
    <a href="qpopupmenu.html">QPopupMenu</a>* popup;
    popup = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    popup-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Quit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
    menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;File", popup );

    // ...and tell the layout about it.
<a name="x1335"></a>    topLayout-&gt;<a href="qlayout.html#setMenuBar">setMenuBar</a>( menubar );

    // Make an hbox that will hold a row of buttons.
    <a href="qboxlayout.html">QBoxLayout</a> *buttons = new <a href="qhboxlayout.html">QHBoxLayout</a>( topLayout );
    int i;
    for ( i = 1; i &lt;= 4; i++ ) {
        <a href="qpushbutton.html">QPushButton</a>* but = new <a href="qpushbutton.html">QPushButton</a>( this );
        <a href="qstring.html">QString</a> s;
<a name="x1338"></a>        s.<a href="qstring.html#sprintf">sprintf</a>( "Button %d", i );
<a name="x1327"></a>        but-&gt;<a href="qbutton.html#setText">setText</a>( s );

        // Set horizontal <a href="layout.html#stretch-factor">stretch factor</a> to 10 to let the buttons
        // stretch horizontally. The buttons will not stretch
        // vertically, since bigWidget below will take up vertical
        // stretch.
<a name="x1326"></a>        buttons-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( but, 10 );
        // (Actually, the result would have been the same with a
        // stretch factor of 0; if no items in a layout have non-zero
        // stretch, the space is divided equally between members.)
    }

    // Make another hbox that will hold a left-justified row of buttons.
    <a href="qboxlayout.html">QBoxLayout</a> *buttons2 = new <a href="qhboxlayout.html">QHBoxLayout</a>( topLayout );

    <a href="qpushbutton.html">QPushButton</a>* but = new <a href="qpushbutton.html">QPushButton</a>( "Button five", this );
    buttons2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( but );

    but = new <a href="qpushbutton.html">QPushButton</a>( "Button 6", this );
    buttons2-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( but );

    // Fill up the rest of the hbox with stretchable space, so that
    // the buttons get their minimum width and are pushed to the left.
<a name="x1325"></a>    buttons2-&gt;<a href="qboxlayout.html#addStretch">addStretch</a>( 10 );

    // Make  a big widget that will grab all space in the middle.
    <a href="qmultilineedit.html">QMultiLineEdit</a> *bigWidget = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this );
    bigWidget-&gt;<a href="qtextedit.html#setText">setText</a>( "This widget will get all the remaining space" );
<a name="x1328"></a>    bigWidget-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Plain );

    // Set vertical stretch factor to 10 to let the bigWidget stretch
    // vertically. It will stretch horizontally because there are no
    // widgets beside it to take up horizontal stretch.
    //    topLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( bigWidget, 10 );
    topLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( bigWidget );

    // Make a grid that will hold a vertical table of QLabel/QLineEdit
    // pairs next to a large QMultiLineEdit.

    // Don't use hard-coded row/column numbers in QGridLayout, you'll
    // regret it when you have to change the layout.
    const int numRows = 3;
    const int labelCol = 0;
    const int linedCol = 1;
    const int multiCol = 2;

    // Let the grid-layout have a spacing of 10 pixels between
    // widgets, overriding the default from topLayout.
    <a href="qgridlayout.html">QGridLayout</a> *grid = new <a href="qgridlayout.html">QGridLayout</a>( topLayout, 0, 0, 10 );
    int row;

    for ( row = 0; row &lt; numRows; row++ ) {
        <a href="qlineedit.html">QLineEdit</a> *ed = new <a href="qlineedit.html">QLineEdit</a>( this );
        // The line edit goes in the second column
<a name="x1330"></a>        grid-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>( ed, row, linedCol );

        // Make a label that is a buddy of the line edit
        <a href="qstring.html">QString</a> s;
        s.<a href="qstring.html#sprintf">sprintf</a>( "Line &amp;%d", row+1 );
        <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>( ed, s, this );
        // The label goes in the first column.
        grid-&gt;<a href="qgridlayout.html#addWidget">addWidget</a>( label, row, labelCol );
    }

    // The multiline edit will cover the entire vertical range of the
    // grid (rows 0 to numRows) and stay in column 2.

    <a href="qmultilineedit.html">QMultiLineEdit</a> *med = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this );
<a name="x1329"></a>    grid-&gt;<a href="qgridlayout.html#addMultiCellWidget">addMultiCellWidget</a>( med, 0, -1, multiCol, multiCol );

    // The labels will take the space they need. Let the remaining
    // horizontal space be shared so that the multiline edit gets
    // twice as much as the line edit.
<a name="x1331"></a>    grid-&gt;<a href="qgridlayout.html#setColStretch">setColStretch</a>( linedCol, 10 );
    grid-&gt;<a href="qgridlayout.html#setColStretch">setColStretch</a>( multiCol, 20 );

    // Add a widget at the bottom.
    <a href="qlabel.html">QLabel</a>* sb = new <a href="qlabel.html">QLabel</a>( this );
    sb-&gt;<a href="qlabel.html#setText">setText</a>( "Let's pretend this is a status bar" );
    sb-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Panel | QFrame::Sunken );
    // This widget will use all horizontal space, and have a fixed height.

    // we should have made a subclass and implemented sizePolicy there...
<a name="x1341"></a>    sb-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( sb-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().height() );

<a name="x1332"></a>    sb-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( AlignVCenter | AlignLeft );
    topLayout-&gt;<a href="qboxlayout.html#addWidget">addWidget</a>( sb );

<a name="x1334"></a>    topLayout-&gt;<a href="qlayout.html#activate">activate</a>();
}

ExampleWidget::~ExampleWidget()
{
    // All child widgets are deleted by Qt.
    // The top-level layout and all its sub-layouts are deleted by Qt.
}

int main( int argc, char **argv )
{
    <a href="qapplication.html">QApplication</a> a( argc, argv );

    ExampleWidget f;
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>(&amp;f);
    f.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Caption");
    f.<a href="qwidget.html#show">show</a>();

    return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>