1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/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"><!--
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 Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped 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 TQt's layout
classes, <a href="qgridlayout.html">TQGridLayout</a>, <a href="qboxlayout.html">TQBoxLayout</a> etc.
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/layout.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include <<a href="qapplication-h.html">ntqapplication.h</a>>
#include <<a href="qlabel-h.html">ntqlabel.h</a>>
#include <<a href="qcolor-h.html">ntqcolor.h</a>>
#include <<a href="qpushbutton-h.html">ntqpushbutton.h</a>>
#include <<a href="qlayout-h.html">ntqlayout.h</a>>
#include <<a href="qlineedit-h.html">ntqlineedit.h</a>>
#include <<a href="qmultilineedit-h.html">ntqmultilineedit.h</a>>
#include <<a href="tqmenubar-h.html">tqmenubar.h</a>>
#include <<a href="tqpopupmenu-h.html">tqpopupmenu.h</a>>
class ExampleWidget : public <a href="tqwidget.html">TQWidget</a>
{
public:
ExampleWidget( <a href="tqwidget.html">TQWidget</a> *parent = 0, const char *name = 0 );
~ExampleWidget();
};
<a name="f260"></a>ExampleWidget::ExampleWidget( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
: <a href="tqwidget.html">TQWidget</a>( parent, name )
{
// Make the top-level layout; a vertical box to contain all widgets
// and sub-layouts.
<a href="qboxlayout.html">TQBoxLayout</a> *topLayout = new <a href="qvboxlayout.html">TQVBoxLayout</a>( this, 5 );
// Create a menubar...
<a href="tqmenubar.html">TQMenuBar</a> *menubar = new <a href="tqmenubar.html">TQMenuBar</a>( this );
<a name="x540"></a> menubar-><a href="tqmenubar.html#setSeparator">setSeparator</a>( TQMenuBar::InWindowsStyle );
<a href="tqpopupmenu.html">TQPopupMenu</a>* popup;
popup = new <a href="tqpopupmenu.html">TQPopupMenu</a>( this );
popup-><a href="tqmenudata.html#insertItem">insertItem</a>( "&Quit", tqApp, TQ_SLOT(<a href="ntqapplication.html#quit">quit</a>()) );
menubar-><a href="tqmenudata.html#insertItem">insertItem</a>( "&File", popup );
// ...and tell the layout about it.
<a name="x539"></a> topLayout-><a href="ntqlayout.html#setMenuBar">setMenuBar</a>( menubar );
// Make an hbox that will hold a row of buttons.
<a href="qboxlayout.html">TQBoxLayout</a> *buttons = new <a href="qhboxlayout.html">TQHBoxLayout</a>( topLayout );
int i;
for ( i = 1; i <= 4; i++ ) {
<a href="ntqpushbutton.html">TQPushButton</a>* but = new <a href="ntqpushbutton.html">TQPushButton</a>( this );
<a href="tqstring.html">TQString</a> s;
<a name="x542"></a> s.<a href="tqstring.html#sprintf">sprintf</a>( "Button %d", i );
<a name="x530"></a> but-><a href="ntqbutton.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="x529"></a> buttons-><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">TQBoxLayout</a> *buttons2 = new <a href="qhboxlayout.html">TQHBoxLayout</a>( topLayout );
<a href="ntqpushbutton.html">TQPushButton</a>* but = new <a href="ntqpushbutton.html">TQPushButton</a>( "Button five", this );
buttons2-><a href="qboxlayout.html#addWidget">addWidget</a>( but );
but = new <a href="ntqpushbutton.html">TQPushButton</a>( "Button 6", this );
buttons2-><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="x528"></a> buttons2-><a href="qboxlayout.html#addStretch">addStretch</a>( 10 );
// Make a big widget that will grab all space in the middle.
<a href="ntqmultilineedit.html">TQMultiLineEdit</a> *bigWidget = new <a href="ntqmultilineedit.html">TQMultiLineEdit</a>( this );
bigWidget-><a href="tqtextedit.html#setText">setText</a>( "This widget will get all the remaining space" );
<a name="x531"></a> bigWidget-><a href="ntqframe.html#setFrameStyle">setFrameStyle</a>( TQFrame::Panel | TQFrame::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-><a href="qboxlayout.html#addWidget">addWidget</a>( bigWidget, 10 );
topLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( bigWidget );
// Make a grid that will hold a vertical table of TQLabel/TQLineEdit
// pairs next to a large TQMultiLineEdit.
// Don't use hard-coded row/column numbers in TQGridLayout, 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">TQGridLayout</a> *grid = new <a href="qgridlayout.html">TQGridLayout</a>( topLayout, 0, 0, 10 );
int row;
for ( row = 0; row < numRows; row++ ) {
<a href="ntqlineedit.html">TQLineEdit</a> *ed = new <a href="ntqlineedit.html">TQLineEdit</a>( this );
// The line edit goes in the second column
<a name="x533"></a> grid-><a href="qgridlayout.html#addWidget">addWidget</a>( ed, row, linedCol );
// Make a label that is a buddy of the line edit
<a href="tqstring.html">TQString</a> s;
s.<a href="tqstring.html#sprintf">sprintf</a>( "Line &%d", row+1 );
<a href="ntqlabel.html">TQLabel</a> *label = new <a href="ntqlabel.html">TQLabel</a>( ed, s, this );
// The label goes in the first column.
grid-><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="ntqmultilineedit.html">TQMultiLineEdit</a> *med = new <a href="ntqmultilineedit.html">TQMultiLineEdit</a>( this );
<a name="x532"></a> grid-><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="x534"></a> grid-><a href="qgridlayout.html#setColStretch">setColStretch</a>( linedCol, 10 );
grid-><a href="qgridlayout.html#setColStretch">setColStretch</a>( multiCol, 20 );
// Add a widget at the bottom.
<a href="ntqlabel.html">TQLabel</a>* sb = new <a href="ntqlabel.html">TQLabel</a>( this );
sb-><a href="ntqlabel.html#setText">setText</a>( "Let's pretend this is a status bar" );
sb-><a href="ntqframe.html#setFrameStyle">setFrameStyle</a>( TQFrame::Panel | TQFrame::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="x545"></a><a name="x537"></a> sb-><a href="tqwidget.html#setFixedHeight">setFixedHeight</a>( sb-><a href="tqwidget.html#sizeHint">sizeHint</a>().height() );
<a name="x535"></a> sb-><a href="ntqlabel.html#setAlignment">setAlignment</a>( AlignVCenter | AlignLeft );
topLayout-><a href="qboxlayout.html#addWidget">addWidget</a>( sb );
<a name="x538"></a> topLayout-><a href="ntqlayout.html#activate">activate</a>();
}
ExampleWidget::~ExampleWidget()
{
// All child widgets are deleted by TQt.
// The top-level layout and all its sub-layouts are deleted by TQt.
}
int main( int argc, char **argv )
{
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
ExampleWidget f;
a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>(&f);
f.<a href="tqwidget.html#setCaption">setCaption</a>("TQt Example - Layouts");
f.<a href="tqwidget.html#show">show</a>();
return a.<a href="ntqapplication.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 © 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>
|