source: trunk/doc/html/menu-example.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 19.3 KB
Line 
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/menu/menu.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Using menus</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>Using menus</h1>
33
34
35<p>
36This example demonstrates a menu bar with pull-down menus,
37sub-menus and custom menu items. It also demonstrates a pop-up
38context menu.
39<p> <hr>
40<p> Header file:
41<p> <pre>/****************************************************************************
42** $Id: menu-example.html 2051 2007-02-21 10:04:20Z chehrlic $
43**
44** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
45**
46** This file is part of an example program for Qt. This example
47** program may be used, distributed and modified without limitation.
48**
49*****************************************************************************/
50
51#ifndef MENU_H
52#define MENU_H
53
54#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
55#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
56#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
57
58
59class MenuExample : public <a href="qwidget.html">QWidget</a>
60{
61 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
62public:
63 MenuExample( <a href="qwidget.html">QWidget</a> *parent=0, const char *name=0 );
64
65public slots:
66 void open();
67 void news();
68 void save();
69 void closeDoc();
70 void undo();
71 void redo();
72 void normal();
73 void bold();
74 void underline();
75 void about();
76 void aboutQt();
77 void printer();
78 void file();
79 void fax();
80 void printerSetup();
81
82protected:
83 void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
84
85signals:
86 void explain( const <a href="qstring.html">QString</a>&amp; );
87
88private:
89 void contextMenuEvent ( <a href="qcontextmenuevent.html">QContextMenuEvent</a> * );
90
91
92 <a href="qmenubar.html">QMenuBar</a> *menu;
93 <a href="qlabel.html">QLabel</a> *label;
94 bool isBold;
95 bool isUnderline;
96 int boldID, underlineID;
97};
98
99
100#endif // MENU_H
101</pre>
102
103<p> <hr>
104<p> Implementation:
105<p> <pre>/****************************************************************************
106** $Id: menu-example.html 2051 2007-02-21 10:04:20Z chehrlic $
107**
108** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
109**
110** This file is part of an example program for Qt. This example
111** program may be used, distributed and modified without limitation.
112**
113*****************************************************************************/
114
115#include "menu.h"
116#include &lt;<a href="qcursor-h.html">qcursor.h</a>&gt;
117#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
118#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
119#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
120#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
121#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
122
123/* XPM */
124static const char * p1_xpm[] = {
125"16 16 3 1",
126" c None",
127". c #000000000000",
128"X c #FFFFFFFF0000",
129" ",
130" ",
131" .... ",
132" .XXXX. ",
133" .............. ",
134" .XXXXXXXXXXXX. ",
135" .XXXXXXXXXXXX. ",
136" .XXXXXXXXXXXX. ",
137" .XXXXXXXXXXXX. ",
138" .XXXXXXXXXXXX. ",
139" .XXXXXXXXXXXX. ",
140" .XXXXXXXXXXXX. ",
141" .XXXXXXXXXXXX. ",
142" .XXXXXXXXXXXX. ",
143" .............. ",
144" "};
145
146/* XPM */
147static const char * p2_xpm[] = {
148"16 16 3 1",
149" c None",
150". c #000000000000",
151"X c #FFFFFFFFFFFF",
152" ",
153" ...... ",
154" .XXX.X. ",
155" .XXX.XX. ",
156" .XXX.XXX. ",
157" .XXX..... ",
158" .XXXXXXX. ",
159" .XXXXXXX. ",
160" .XXXXXXX. ",
161" .XXXXXXX. ",
162" .XXXXXXX. ",
163" .XXXXXXX. ",
164" .XXXXXXX. ",
165" ......... ",
166" ",
167" "};
168
169/* XPM */
170static const char * p3_xpm[] = {
171"16 16 3 1",
172" c None",
173". c #000000000000",
174"X c #FFFFFFFFFFFF",
175" ",
176" ",
177" ......... ",
178" ........... ",
179" ........ .. ",
180" ........... ",
181" ........... ",
182" ........... ",
183" ........... ",
184" ...XXXXX... ",
185" ...XXXXX... ",
186" ...XXXXX... ",
187" ...XXXXX... ",
188" ......... ",
189" ",
190" "};
191
192
193/*
194 Auxiliary class to provide fancy menu items with different
195 fonts. Used for the "bold" and "underline" menu items in the options
196 menu.
197 */
198class MyMenuItem : public <a href="qcustommenuitem.html">QCustomMenuItem</a>
199{
200public:
201 MyMenuItem( const <a href="qstring.html">QString</a>&amp; s, const <a href="qfont.html">QFont</a>&amp; f )
202 : string( s ), font( f ){};
203 ~MyMenuItem(){}
204
205 void paint( <a href="qpainter.html">QPainter</a>* p, const QColorGroup&amp; /*cg*/, bool /*act*/, bool /*enabled*/, int x, int y, int w, int h )
206 {
207 p-&gt;<a href="qpainter.html#setFont">setFont</a> ( font );
208 p-&gt;<a href="qpainter.html#drawText">drawText</a>( x, y, w, h, AlignLeft | AlignVCenter | DontClip | ShowPrefix, string );
209 }
210
211 <a href="qsize.html">QSize</a> sizeHint()
212 {
213 return QFontMetrics( font ).size( AlignLeft | AlignVCenter | ShowPrefix | DontClip, string );
214 }
215private:
216 <a href="qstring.html">QString</a> string;
217 <a href="qfont.html">QFont</a> font;
218};
219
220
221<a name="f501"></a>MenuExample::MenuExample( <a href="qwidget.html">QWidget</a> *parent, const char *name )
222 : <a href="qwidget.html">QWidget</a>( parent, name )
223{
224 <a href="qpixmap.html">QPixmap</a> p1( p1_xpm );
225 <a href="qpixmap.html">QPixmap</a> p2( p2_xpm );
226 <a href="qpixmap.html">QPixmap</a> p3( p3_xpm );
227 <a href="qpopupmenu.html">QPopupMenu</a> *print = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
228 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( print );
229<a name="x1879"></a> print-&gt;<a href="qpopupmenu.html#insertTearOffHandle">insertTearOffHandle</a>();
230<a name="x1869"></a> print-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Print to printer", this, SLOT(printer()) );
231 print-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Print to &amp;file", this, SLOT(file()) );
232 print-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Print to fa&amp;x", this, SLOT(fax()) );
233<a name="x1870"></a> print-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
234 print-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Printer &amp;Setup", this, SLOT(printerSetup()) );
235
236 <a href="qpopupmenu.html">QPopupMenu</a> *file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
237 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( file );
238 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( p1, "&amp;Open", this, SLOT(open()), CTRL+Key_O );
239 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( p2, "&amp;New", this, SLOT(news()), CTRL+Key_N );
240 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( p3, "&amp;Save", this, SLOT(save()), CTRL+Key_S );
241 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Close", this, SLOT(closeDoc()), CTRL+Key_W );
242 file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
243 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Print", print, CTRL+Key_P );
244 file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
245 file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "E&amp;xit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()), CTRL+Key_Q );
246
247 <a href="qpopupmenu.html">QPopupMenu</a> *edit = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
248 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( edit );
249 int undoID = edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Undo", this, SLOT(undo()) );
250 int redoID = edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Redo", this, SLOT(redo()) );
251<a name="x1873"></a> edit-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( undoID, FALSE );
252 edit-&gt;<a href="qmenudata.html#setItemEnabled">setItemEnabled</a>( redoID, FALSE );
253
254 <a href="qpopupmenu.html">QPopupMenu</a>* options = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
255 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( options );
256 options-&gt;<a href="qpopupmenu.html#insertTearOffHandle">insertTearOffHandle</a>();
257 options-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Options");
258 options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Normal Font", this, SLOT(normal()) );
259 options-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
260
261<a name="x1883"></a> options-&gt;<a href="qwidget.html#polish">polish</a>(); // adjust system settings
262<a name="x1882"></a> <a href="qfont.html">QFont</a> f = options-&gt;<a href="qwidget.html#font">font</a>();
263<a name="x1860"></a> f.<a href="qfont.html#setBold">setBold</a>( TRUE );
264 boldID = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( new MyMenuItem( "Bold", f ) );
265<a name="x1871"></a> options-&gt;<a href="qmenudata.html#setAccel">setAccel</a>( CTRL+Key_B, boldID );
266<a name="x1868"></a> options-&gt;<a href="qmenudata.html#connectItem">connectItem</a>( boldID, this, SLOT(bold()) );
267 f = <a href="qwidget.html#font">font</a>();
268<a name="x1861"></a> f.<a href="qfont.html#setUnderline">setUnderline</a>( TRUE );
269 underlineID = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( new MyMenuItem( "Underline", f ) );
270 options-&gt;<a href="qmenudata.html#setAccel">setAccel</a>( CTRL+Key_U, underlineID );
271 options-&gt;<a href="qmenudata.html#connectItem">connectItem</a>( underlineID, this, SLOT(underline()) );
272
273 isBold = FALSE;
274 isUnderline = FALSE;
275<a name="x1880"></a> options-&gt;<a href="qpopupmenu.html#setCheckable">setCheckable</a>( TRUE );
276
277
278 <a href="qpopupmenu.html">QPopupMenu</a> *help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
279 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( help );
280 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;About", this, SLOT(about()), CTRL+Key_H );
281 help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About &amp;Qt", this, SLOT(aboutQt()) );
282
283 // If we used a QMainWindow we could use its built-in menuBar().
284 menu = new <a href="qmenubar.html">QMenuBar</a>( this );
285 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( menu );
286 menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;File", file );
287 menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Edit", edit );
288 menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Options", options );
289 menu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
290 menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Help", help );
291<a name="x1867"></a> menu-&gt;<a href="qmenubar.html#setSeparator">setSeparator</a>( QMenuBar::InWindowsStyle );
292
293
294 <a href="qlabel.html">QLabel</a> *msg = new <a href="qlabel.html">QLabel</a>( this );
295 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( msg );
296 msg-&gt;<a href="qlabel.html#setText">setText</a>( "A context menu is available.\n"
297 "Invoke it by right-clicking or by"
298 " pressing the 'context' button." );
299<a name="x1886"></a> msg-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>( 0, height() - 60, width(), 60 );
300<a name="x1864"></a> msg-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( AlignCenter );
301
302 label = new <a href="qlabel.html">QLabel</a>( this );
303 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( label );
304 label-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>( 20, rect().center().y()-20, width()-40, 40 );
305 label-&gt;<a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::Box | QFrame::Raised );
306<a name="x1863"></a> label-&gt;<a href="qframe.html#setLineWidth">setLineWidth</a>( 1 );
307 label-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( AlignCenter );
308
309 <a href="qobject.html#connect">connect</a>( this, SIGNAL(explain(const <a href="qstring.html">QString</a>&amp;)),
310 label, SLOT(<a href="qlabel.html#setText">setText</a>(const <a href="qstring.html">QString</a>&amp;)) );
311
312 <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 100, 80 );
313 <a href="qwidget.html#setFocusPolicy">setFocusPolicy</a>( QWidget::ClickFocus );
314}
315
316
317<a name="x1881"></a>void MenuExample::<a href="qwidget.html#contextMenuEvent">contextMenuEvent</a>( <a href="qcontextmenuevent.html">QContextMenuEvent</a> * )
318{
319 <a href="qpopupmenu.html">QPopupMenu</a>* contextMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
320 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( contextMenu );
321 <a href="qlabel.html">QLabel</a> *caption = new <a href="qlabel.html">QLabel</a>( "&lt;font color=darkblue&gt;&lt;u&gt;&lt;b&gt;"
322 "Context Menu&lt;/b&gt;&lt;/u&gt;&lt;/font&gt;", this );
323 caption-&gt;<a href="qlabel.html#setAlignment">setAlignment</a>( Qt::AlignCenter );
324 contextMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( caption );
325 contextMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;New", this, SLOT(news()), CTRL+Key_N );
326 contextMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Open...", this, SLOT(open()), CTRL+Key_O );
327 contextMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Save", this, SLOT(save()), CTRL+Key_S );
328 <a href="qpopupmenu.html">QPopupMenu</a> *submenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
329 <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( submenu );
330 submenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Print to printer", this, SLOT(printer()) );
331 submenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Print to &amp;file", this, SLOT(file()) );
332 submenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "Print to fa&amp;x", this, SLOT(fax()) );
333 contextMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Print", submenu );
334<a name="x1878"></a><a name="x1859"></a> contextMenu-&gt;<a href="qpopupmenu.html#exec">exec</a>( QCursor::<a href="qcursor.html#pos">pos</a>() );
335 delete contextMenu;
336}
337
338
339void <a name="f502"></a>MenuExample::open()
340{
341 emit explain( "File/Open selected" );
342}
343
344
345void <a name="f503"></a>MenuExample::news()
346{
347 emit explain( "File/New selected" );
348}
349
350void <a name="f504"></a>MenuExample::save()
351{
352 emit explain( "File/Save selected" );
353}
354
355
356void <a name="f505"></a>MenuExample::closeDoc()
357{
358 emit explain( "File/Close selected" );
359}
360
361
362void <a name="f506"></a>MenuExample::undo()
363{
364 emit explain( "Edit/Undo selected" );
365}
366
367
368void <a name="f507"></a>MenuExample::redo()
369{
370 emit explain( "Edit/Redo selected" );
371}
372
373
374void <a name="f508"></a>MenuExample::normal()
375{
376 isBold = FALSE;
377 isUnderline = FALSE;
378 <a href="qfont.html">QFont</a> font;
379<a name="x1865"></a> label-&gt;<a href="qlabel.html#setFont">setFont</a>( font );
380<a name="x1872"></a> menu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( boldID, isBold );
381 menu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( underlineID, isUnderline );
382 emit explain( "Options/Normal selected" );
383}
384
385
386void <a name="f509"></a>MenuExample::bold()
387{
388 isBold = !isBold;
389 <a href="qfont.html">QFont</a> font;
390 font.<a href="qfont.html#setBold">setBold</a>( isBold );
391 font.<a href="qfont.html#setUnderline">setUnderline</a>( isUnderline );
392 label-&gt;<a href="qlabel.html#setFont">setFont</a>( font );
393 menu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( boldID, isBold );
394 emit explain( "Options/Bold selected" );
395}
396
397
398void <a name="f510"></a>MenuExample::underline()
399{
400 isUnderline = !isUnderline;
401 <a href="qfont.html">QFont</a> font;
402 font.<a href="qfont.html#setBold">setBold</a>( isBold );
403 font.<a href="qfont.html#setUnderline">setUnderline</a>( isUnderline );
404 label-&gt;<a href="qlabel.html#setFont">setFont</a>( font );
405 menu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>( underlineID, isUnderline );
406 emit explain( "Options/Underline selected" );
407}
408
409
410void <a name="f511"></a>MenuExample::about()
411{
412<a name="x1874"></a> QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Qt Menu Example",
413 "This example demonstrates simple use of Qt menus.\n"
414 "You can cut and paste lines from it to your own\n"
415 "programs." );
416}
417
418
419void <a name="f512"></a>MenuExample::aboutQt()
420{
421<a name="x1875"></a> QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Menu Example" );
422}
423
424
425void <a name="f513"></a>MenuExample::printer()
426{
427 emit explain( "File/Printer/Print selected" );
428}
429
430void <a name="f514"></a>MenuExample::file()
431{
432 emit explain( "File/Printer/Print To File selected" );
433}
434
435void <a name="f515"></a>MenuExample::fax()
436{
437 emit explain( "File/Printer/Print To Fax selected" );
438}
439
440void <a name="f516"></a>MenuExample::printerSetup()
441{
442 emit explain( "File/Printer/Printer Setup selected" );
443}
444
445
446<a name="x1884"></a>void MenuExample::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
447{
448 label-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>( 20, rect().center().y()-20, width()-40, 40 );
449}
450
451
452int main( int argc, char ** argv )
453{
454 <a href="qapplication.html">QApplication</a> a( argc, argv );
455 MenuExample m;
456 m.<a href="qwidget.html#setCaption">setCaption</a>("Qt Examples - Menus");
457 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;m );
458 m.<a href="qwidget.html#show">show</a>();
459 return a.<a href="qapplication.html#exec">exec</a>();
460}
461</pre>
462
463<p>See also <a href="examples.html">Examples</a>.
464
465<!-- eof -->
466<p><address><hr><div align=center>
467<table width=100% cellspacing=0 border=0><tr>
468<td>Copyright &copy; 2007
469<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
470<td align=right><div align=right>Qt 3.3.8</div>
471</table></div></address></body>
472</html>
Note: See TracBrowser for help on using the repository browser.