File: menu-menu-cpp.html

package info (click to toggle)
qt-embedded 2.3.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 68,608 kB
  • ctags: 45,998
  • sloc: cpp: 276,654; ansic: 71,987; makefile: 29,074; sh: 12,305; yacc: 2,465; python: 1,863; perl: 481; lex: 480; xml: 68; lisp: 15
file content (413 lines) | stat: -rw-r--r-- 16,155 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!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 - menu/menu.cpp example file
</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">

<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></a>
</div>
</table>

<h1 align=center>Using menus</h1><br clear="all">

  This example demonstrates simple use of menus (a menu bar and
  pull-down menus).  Qt also supports popup menus, but this example
  doesn't contain any.

  <blockquote>
  Customer: "My computer crashed!"<br>
  Tech Support: "It crashed?"<br>
  Customer: "Yeah, it won't let me play my game."<br>
  Tech Support: "All right, hit Control-Alt-Delete to reboot."<br>
  Customer: "No, it didn't crash -- it crashed."<br>
  Tech Support: "Huh?"<br>
  Customer: "I crashed my game. That's what I said before. I crashed my spaceship and now it doesn't work."<br>
  Tech Support: "Click on 'File,' then 'New Game.'"<br>
  Customer: [pause] "Wow! How'd you learn how to do that?"<br>
  </blockquote>

  Now you too can write menu trees tech support will know how to use!

  <hr>

  Header file: <pre>/****************************************************************************
** &#36;Id&#58; qt/examples/menu/menu.h   2.3.2   edited 2001-01-26 $
**
** 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.
**
*****************************************************************************/

#ifndef MENU_H
#define MENU_H

#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;

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

public slots:
    void open();
    void news();
    void save();
    void closeDoc();
    void undo();
    void redo();
    void normal();
    void bold();
    void underline();
    void about();
    void aboutQt();
    void printer();
    void file();
    void fax();
    void printerSetup();

protected:
    void    resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );

signals:
    void    explain( const QString&amp; );

private:
    <a href="qmenubar.html">QMenuBar</a> *menu;
    <a href="qlabel.html">QLabel</a>   *label;
    bool isBold;
    bool isUnderline;
    int boldID, underlineID;
};

#endif // MENU_H
</pre>


  <hr>

  Implementation:
<pre>/****************************************************************************
** &#36;Id&#58; qt/examples/menu/menu.cpp   2.3.2   edited 2001-06-12 $
**
** 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 "menu.h"
#include &lt;<a name="qpopupmenu.h"></a><a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;qkeycode.h&gt;
#include &lt;<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a name="qmessagebox.h"></a><a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a name="qpixmap.h"></a><a href="qpixmap-h.html">qpixmap.h</a>&gt;
#include &lt;<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>&gt;

/* XPM */
static const char * p1_xpm[] = {
"16 16 3 1",
"       c None",
".      c #000000000000",
"X      c #FFFFFFFF0000",
"                ",
"                ",
"         ....   ",
"        .XXXX.  ",
" .............. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .............. ",
"                "};

/* XPM */
static const char * p2_xpm[] = {
"16 16 3 1",
"       c None",
".      c #000000000000",
"X      c #FFFFFFFFFFFF",
"                ",
"   ......       ",
"   .XXX.X.      ",
"   .XXX.XX.     ",
"   .XXX.XXX.    ",
"   .XXX.....    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .........    ",
"                ",
"                "};

/* XPM */
static const char * p3_xpm[] = {
"16 16 3 1",
"       c None",
".      c #000000000000",
"X      c #FFFFFFFFFFFF",
"                ",
"                ",
"   .........    ",
"  ...........   ",
"  ........ ..   ",
"  ...........   ",
"  ...........   ",
"  ...........   ",
"  ...........   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"   .........    ",
"                ",
"                "};

/*
  Auxiliary class to provide fancy menu items with different
  fonts. Used for the "bold" and "underline" menu items in the options
  menu.
 */
class MyMenuItem : public QCustomMenuItem
{
public:
    MyMenuItem( const QString&amp; s, const QFont&amp; f )
        : string( s ), font( f ){};
    ~MyMenuItem(){}

    void paint( <a name="QPainter"></a><a href="qpainter.html">QPainter</a>* p, const QColorGroup&amp; /*cg*/, bool /*act*/, bool /*enabled*/, int x, int y, int w, int h )
    {
        p-&gt;<a name="setFont"></a><a href="qpainter.html#998df2">setFont</a> ( font );
        p-&gt;<a name="drawText"></a><a href="qpainter.html#0f088f">drawText</a>( x, y, w, h, AlignLeft | AlignVCenter | ShowPrefix | DontClip, string );
    }

    <a name="QSize"></a><a href="qsize.html">QSize</a> sizeHint()
    {
        return QFontMetrics( font ).size( AlignLeft | AlignVCenter | ShowPrefix | DontClip,  string );
    }
private:
    <a name="QString"></a><a href="qstring.html">QString</a> string;
    <a name="QFont"></a><a href="qfont.html">QFont</a> font;
};

MenuExample::MenuExample( <a name="QWidget"></a><a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qwidget.html">QWidget</a>( parent, name )
{
    <a name="QPixmap"></a><a href="qpixmap.html">QPixmap</a> p1( p1_xpm );
    <a href="qpixmap.html">QPixmap</a> p2( p2_xpm );
    <a href="qpixmap.html">QPixmap</a> p3( p3_xpm );

    <a name="QPopupMenu"></a><a href="qpopupmenu.html">QPopupMenu</a> *print = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    CHECK_PTR( print );
    print-&gt;<a name="insertTearOffHandle"></a><a href="qpopupmenu.html#27032b">insertTearOffHandle</a>();
    print-&gt;<a name="insertItem"></a><a href="qmenudata.html#0076cb">insertItem</a>( "&amp;Print to printer", this, SLOT(<a name="printer"></a><a href="#274">printer</a>()) );
    print-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "Print to &amp;file", this, SLOT(<a name="file"></a><a href="#275">file</a>()) );
    print-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "Print to fa&amp;x", this, SLOT(<a name="fax"></a><a href="#276">fax</a>()) );
    print-&gt;<a name="insertSeparator"></a><a href="qmenudata.html#e34b79">insertSeparator</a>();
    print-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "Printer &amp;Setup", this, SLOT(<a name="printerSetup"></a><a href="#277">printerSetup</a>()) );

    <a href="qpopupmenu.html">QPopupMenu</a> *file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    CHECK_PTR( file );
    file-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( p1, "&amp;Open",  this, SLOT(<a name="open"></a><a href="#263">open</a>()), CTRL+Key_O );
    file-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( p2, "&amp;New", this, SLOT(<a name="news"></a><a href="#264">news</a>()), CTRL+Key_N );
    file-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( p3, "&amp;Save", this, SLOT(<a name="save"></a><a href="#265">save</a>()), CTRL+Key_S );
    file-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "&amp;Close", this, SLOT(<a name="closeDoc"></a><a href="#266">closeDoc</a>()), CTRL+Key_W );
    file-&gt;<a href="qmenudata.html#e34b79">insertSeparator</a>();
    file-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "&amp;Print", print, CTRL+Key_P );
    file-&gt;<a href="qmenudata.html#e34b79">insertSeparator</a>();
    file-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "E&amp;xit",  qApp, SLOT(quit()), CTRL+Key_Q );

    <a href="qpopupmenu.html">QPopupMenu</a> *edit = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    CHECK_PTR( edit );
    int undoID = edit-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "&amp;Undo", this, SLOT(<a name="undo"></a><a href="#267">undo</a>()) );
    int redoID = edit-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "&amp;Redo", this, SLOT(<a name="redo"></a><a href="#268">redo</a>()) );
    edit-&gt;<a name="setItemEnabled"></a><a href="qmenudata.html#c9a23c">setItemEnabled</a>( undoID, FALSE );
    edit-&gt;<a href="qmenudata.html#c9a23c">setItemEnabled</a>( redoID, FALSE );

    <a href="qpopupmenu.html">QPopupMenu</a>* options = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    CHECK_PTR( options );
    options-&gt;<a href="qpopupmenu.html#27032b">insertTearOffHandle</a>();
    options-&gt;<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>("Options");
    options-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "&amp;Normal Font", this, SLOT(<a name="normal"></a><a href="#269">normal</a>()) );
    options-&gt;<a href="qmenudata.html#e34b79">insertSeparator</a>();

    options-&gt;<a name="polish"></a><a href="qwidget.html#c14a09">polish</a>(); // adjust system settings 
    <a href="qfont.html">QFont</a> f = options-&gt;<a name="font"></a><a href="qwidget.html#167922">font</a>();
    f.<a name="setBold"></a><a href="qfont.html#ec241e">setBold</a>( TRUE );
    boldID = options-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( new MyMenuItem( "&amp;Bold", f ) );
    options-&gt;<a name="setAccel"></a><a href="qmenudata.html#58b23b">setAccel</a>( CTRL+Key_B, boldID );
    options-&gt;<a name="connectItem"></a><a href="qmenudata.html#d7f757">connectItem</a>( boldID, this, SLOT(<a name="bold"></a><a href="#270">bold</a>()) );
    f = font();
    f.<a name="setUnderline"></a><a href="qfont.html#c11804">setUnderline</a>( TRUE );
    underlineID = options-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( new MyMenuItem( "&amp;Underline", f ) );
    options-&gt;<a href="qmenudata.html#58b23b">setAccel</a>( CTRL+Key_U, underlineID );
    options-&gt;<a href="qmenudata.html#d7f757">connectItem</a>( underlineID, this, SLOT(<a name="underline"></a><a href="#271">underline</a>()) );

    isBold = FALSE;
    isUnderline = FALSE;
    options-&gt;<a name="setCheckable"></a><a href="qpopupmenu.html#672d77">setCheckable</a>( TRUE );

    <a href="qpopupmenu.html">QPopupMenu</a> *help = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    CHECK_PTR( help );
    help-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "&amp;About", this, SLOT(<a name="about"></a><a href="#272">about</a>()), CTRL+Key_H );
    help-&gt;<a href="qmenudata.html#0076cb">insertItem</a>( "About &amp;<a name="Qt"></a><a href="qt.html">Qt</a>", this, SLOT(<a name="aboutQt"></a><a href="#273">aboutQt</a>()) );

    menu = new <a name="QMenuBar"></a><a href="qmenubar.html">QMenuBar</a>( this );
    CHECK_PTR( menu );
    menu-&gt;insertItem( "&amp;File", file );
    menu-&gt;insertItem( "&amp;Edit", edit );
    menu-&gt;insertItem( "&amp;Options", options );
    menu-&gt;insertSeparator();
    menu-&gt;insertItem( "&amp;Help", help );
    menu-&gt;setSeparator( QMenuBar::InWindowsStyle );

    label = new <a name="QLabel"></a><a href="qlabel.html">QLabel</a>( this );
    CHECK_PTR( label );
    label-&gt;setGeometry( 20, <a name="rect"></a><a href="qwidget.html#75ae71">rect</a>().center().y()-20, <a name="width"></a><a href="qwidget.html#2edab1">width</a>()-40, 40 );
    label-&gt;setFrameStyle( QFrame::Box | QFrame::Raised );
    label-&gt;setLineWidth( 1 );
    label-&gt;setAlignment( AlignCenter );

    <a name="connect"></a><a href="qobject.html#7f8e37">connect</a>( this,  SIGNAL(explain(const QString&amp;)),
             label, SLOT(setText(const QString&amp;)) );

    <a name="setMinimumSize"></a><a href="qwidget.html#c0b5fb">setMinimumSize</a>( 100, 80 );
}

void <a name="263"></a>MenuExample::open()
{
    emit explain( "File/Open selected" );
}

void <a name="264"></a>MenuExample::news()
{
    emit explain( "File/New selected" );
}

void <a name="265"></a>MenuExample::save()
{
    emit explain( "File/Save selected" );
}

void <a name="266"></a>MenuExample::closeDoc()
{
    emit explain( "File/Close selected" );
}

void <a name="267"></a>MenuExample::undo()
{
    emit explain( "Edit/Undo selected" );
}

void <a name="268"></a>MenuExample::redo()
{
    emit explain( "Edit/Redo selected" );
}

void <a name="269"></a>MenuExample::normal()
{
    isBold = FALSE;
    isUnderline = FALSE;
    menu-&gt;setItemChecked( boldID, isBold );
    menu-&gt;setItemChecked( underlineID, isUnderline );
    emit explain( "Options/Normal selected" );
}

void <a name="270"></a>MenuExample::bold()
{
    isBold = !isBold;
    menu-&gt;setItemChecked( boldID, isBold );
    emit explain( "Options/Bold selected" );
}

void <a name="271"></a>MenuExample::underline()
{
    isUnderline = !isUnderline;
    menu-&gt;setItemChecked( underlineID, isUnderline );
    emit explain( "Options/Underline selected" );
}

void <a name="272"></a>MenuExample::about()
{
    <a name="QMessageBox::about"></a><a href="qmessagebox.html#f6c3cd">QMessageBox::about</a>( this, "Qt Menu Example",
                        "This example demonstrates simple use of Qt menus.\n"
                        "You can cut and paste lines from it to your own\n"
                        "programs." );
}

void <a name="273"></a>MenuExample::aboutQt()
{
    <a name="QMessageBox::aboutQt"></a><a href="qmessagebox.html#b72270">QMessageBox::aboutQt</a>( this, "Qt Menu Example" );
}

void <a name="274"></a>MenuExample::printer()
{
    emit explain( "File/Printer/Print selected" );
}

void <a name="275"></a>MenuExample::file()
{
    emit explain( "File/Printer/Print To File selected" );
}

void <a name="276"></a>MenuExample::fax()
{
    emit explain( "File/Printer/Print To Fax selected" );
}

void <a name="277"></a>MenuExample::printerSetup()
{
    emit explain( "File/Printer/Printer Setup selected" );
}

void <a name="278"></a>MenuExample::resizeEvent( <a name="QResizeEvent"></a><a href="qresizeevent.html">QResizeEvent</a> * )
{
    label-&gt;setGeometry( 20, <a href="qwidget.html#75ae71">rect</a>().center().y()-20, <a href="qwidget.html#2edab1">width</a>()-40, 40 );
}

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

    a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &amp;m );
    m.<a href="qwidget.html#d6a291">setCaption</a>("Qt Examples - Menus");
    m.<a name="show"></a><a href="qwidget.html#200ee5">show</a>();
    return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright  2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>