~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/mainwindows-menus-mainwindow-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
    <title>Qt 4.0: mainwindow.cpp Example File (mainwindows/menus/mainwindow.cpp)</title>
 
7
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
8
a:link { color: #004faf; text-decoration: none }
 
9
a:visited { color: #672967; text-decoration: none }
 
10
td.postheader { font-family: sans-serif }
 
11
tr.address { font-family: sans-serif }
 
12
body { background: #ffffff; color: black; }</style>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
18
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><small><small>mainwindows/menus/mainwindow.cpp</small></small></h1>
 
20
<pre>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
26
    ** This file may be distributed under the terms of the Q Public License
 
27
** as defined by Trolltech AS of Norway and appearing in the file
 
28
** LICENSE.QPL included in the packaging of this file.
 
29
**
 
30
** This file may be distributed and/or modified under the terms of the
 
31
** GNU General Public License version 2 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.
 
34
**
 
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
36
**   information about Qt Commercial License Agreements.
 
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
43
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
44
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
 
 
50
    #include &quot;mainwindow.h&quot;
 
51
 
 
52
    MainWindow::MainWindow()
 
53
    {
 
54
        QWidget *w = new QWidget;
 
55
        setCentralWidget(w);
 
56
 
 
57
        QWidget *topFiller = new QWidget;
 
58
        topFiller-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
59
 
 
60
        infoLabel = new QLabel(tr(&quot;&lt;i&gt;Choose a menu option, or right-click to &quot;
 
61
                                  &quot;invoke a context menu&lt;/i&gt;&quot;));
 
62
        infoLabel-&gt;setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
 
63
        infoLabel-&gt;setAlignment(Qt::AlignCenter);
 
64
 
 
65
        QWidget *bottomFiller = new QWidget;
 
66
        bottomFiller-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
67
 
 
68
        QVBoxLayout *vbox = new QVBoxLayout;
 
69
        vbox-&gt;setMargin(5);
 
70
        vbox-&gt;addWidget(topFiller);
 
71
        vbox-&gt;addWidget(infoLabel);
 
72
        vbox-&gt;addWidget(bottomFiller);
 
73
        w-&gt;setLayout(vbox);
 
74
 
 
75
        createActions();
 
76
        createMenus();
 
77
 
 
78
        statusBar()-&gt;showMessage(tr(&quot;A context menu is available by right-clicking&quot;));
 
79
 
 
80
        setWindowTitle(tr(&quot;Menus&quot;));
 
81
        setMinimumSize(160, 160);
 
82
        resize(480, 320);
 
83
    }
 
84
 
 
85
    void MainWindow::contextMenuEvent(QContextMenuEvent *event)
 
86
    {
 
87
        QMenu menu(this);
 
88
        menu.addAction(cutAct);
 
89
        menu.addAction(copyAct);
 
90
        menu.addAction(pasteAct);
 
91
        menu.exec(event-&gt;globalPos());
 
92
    }
 
93
 
 
94
    void MainWindow::newFile()
 
95
    {
 
96
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;File|New&lt;/b&gt;&quot;));
 
97
    }
 
98
 
 
99
    void MainWindow::open()
 
100
    {
 
101
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;File|Open&lt;/b&gt;&quot;));
 
102
    }
 
103
 
 
104
    void MainWindow::save()
 
105
    {
 
106
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;File|Save&lt;/b&gt;&quot;));
 
107
    }
 
108
 
 
109
    void MainWindow::print()
 
110
    {
 
111
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;File|Print&lt;/b&gt;&quot;));
 
112
    }
 
113
 
 
114
    void MainWindow::undo()
 
115
    {
 
116
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Undo&lt;/b&gt;&quot;));
 
117
    }
 
118
 
 
119
    void MainWindow::redo()
 
120
    {
 
121
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Redo&lt;/b&gt;&quot;));
 
122
    }
 
123
 
 
124
    void MainWindow::cut()
 
125
    {
 
126
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Cut&lt;/b&gt;&quot;));
 
127
    }
 
128
 
 
129
    void MainWindow::copy()
 
130
    {
 
131
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Copy&lt;/b&gt;&quot;));
 
132
    }
 
133
 
 
134
    void MainWindow::paste()
 
135
    {
 
136
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Paste&lt;/b&gt;&quot;));
 
137
    }
 
138
 
 
139
    void MainWindow::bold()
 
140
    {
 
141
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Bold&lt;/b&gt;&quot;));
 
142
    }
 
143
 
 
144
    void MainWindow::italic()
 
145
    {
 
146
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Italic&lt;/b&gt;&quot;));
 
147
    }
 
148
 
 
149
    void MainWindow::leftAlign()
 
150
    {
 
151
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Left Align&lt;/b&gt;&quot;));
 
152
    }
 
153
 
 
154
    void MainWindow::rightAlign()
 
155
    {
 
156
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Right Align&lt;/b&gt;&quot;));
 
157
    }
 
158
 
 
159
    void MainWindow::justify()
 
160
    {
 
161
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Justify&lt;/b&gt;&quot;));
 
162
    }
 
163
 
 
164
    void MainWindow::center()
 
165
    {
 
166
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Center&lt;/b&gt;&quot;));
 
167
    }
 
168
 
 
169
    void MainWindow::setLineSpacing()
 
170
    {
 
171
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Set Line Spacing&lt;/b&gt;&quot;));
 
172
    }
 
173
 
 
174
    void MainWindow::setParagraphSpacing()
 
175
    {
 
176
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Edit|Format|Set Paragraph Spacing&lt;/b&gt;&quot;));
 
177
    }
 
178
 
 
179
    void MainWindow::about()
 
180
    {
 
181
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Help|About&lt;/b&gt;&quot;));
 
182
        QMessageBox::about(this, tr(&quot;About Menu&quot;),
 
183
                tr(&quot;The &lt;b&gt;Menu&lt;/b&gt; example shows how to create &quot;
 
184
                   &quot;menu-bar menus and context menus.&quot;));
 
185
    }
 
186
 
 
187
    void MainWindow::aboutQt()
 
188
    {
 
189
        infoLabel-&gt;setText(tr(&quot;Invoked &lt;b&gt;Help|About Qt&lt;/b&gt;&quot;));
 
190
    }
 
191
 
 
192
    void MainWindow::createActions()
 
193
    {
 
194
        newAct = new QAction(tr(&quot;&amp;New&quot;), this);
 
195
        newAct-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
 
196
        newAct-&gt;setStatusTip(tr(&quot;Create a new file&quot;));
 
197
        connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 
198
 
 
199
        openAct = new QAction(tr(&quot;&amp;Open...&quot;), this);
 
200
        openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
 
201
        openAct-&gt;setStatusTip(tr(&quot;Open an existing file&quot;));
 
202
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
203
 
 
204
        saveAct = new QAction(tr(&quot;&amp;Save&quot;), this);
 
205
        saveAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
 
206
        saveAct-&gt;setStatusTip(tr(&quot;Save the document to disk&quot;));
 
207
        connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
 
208
 
 
209
        printAct = new QAction(tr(&quot;&amp;Print...&quot;), this);
 
210
        printAct-&gt;setShortcut(tr(&quot;Ctrl+P&quot;));
 
211
        printAct-&gt;setStatusTip(tr(&quot;Print the document&quot;));
 
212
        connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
 
213
 
 
214
        exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
215
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
216
        exitAct-&gt;setStatusTip(tr(&quot;Exit the application&quot;));
 
217
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
218
 
 
219
        undoAct = new QAction(tr(&quot;&amp;Undo&quot;), this);
 
220
        undoAct-&gt;setShortcut(tr(&quot;Ctrl+Z&quot;));
 
221
        undoAct-&gt;setStatusTip(tr(&quot;Undo the last operation&quot;));
 
222
        connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
 
223
 
 
224
        redoAct = new QAction(tr(&quot;&amp;Redo&quot;), this);
 
225
        redoAct-&gt;setShortcut(tr(&quot;Ctrl+Y&quot;));
 
226
        redoAct-&gt;setStatusTip(tr(&quot;Redo the last operation&quot;));
 
227
        connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
 
228
 
 
229
        cutAct = new QAction(tr(&quot;Cu&amp;t&quot;), this);
 
230
        cutAct-&gt;setShortcut(tr(&quot;Ctrl+X&quot;));
 
231
        cutAct-&gt;setStatusTip(tr(&quot;Cut the current selection's contents to the &quot;
 
232
                                &quot;clipboard&quot;));
 
233
        connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
 
234
 
 
235
        copyAct = new QAction(tr(&quot;&amp;Copy&quot;), this);
 
236
        copyAct-&gt;setShortcut(tr(&quot;Ctrl+C&quot;));
 
237
        copyAct-&gt;setStatusTip(tr(&quot;Copy the current selection's contents to the &quot;
 
238
                                 &quot;clipboard&quot;));
 
239
        connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
 
240
 
 
241
        pasteAct = new QAction(tr(&quot;&amp;Paste&quot;), this);
 
242
        pasteAct-&gt;setShortcut(tr(&quot;Ctrl+V&quot;));
 
243
        pasteAct-&gt;setStatusTip(tr(&quot;Paste the clipboard's contents into the current &quot;
 
244
                                  &quot;selection&quot;));
 
245
        connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
 
246
 
 
247
        boldAct = new QAction(tr(&quot;&amp;Bold&quot;), this);
 
248
        boldAct-&gt;setCheckable(true);
 
249
        boldAct-&gt;setShortcut(tr(&quot;Ctrl+B&quot;));
 
250
        boldAct-&gt;setStatusTip(tr(&quot;Make the text bold&quot;));
 
251
        connect(boldAct, SIGNAL(triggered()), this, SLOT(bold()));
 
252
 
 
253
        QFont boldFont = boldAct-&gt;font();
 
254
        boldFont.setBold(true);
 
255
        boldAct-&gt;setFont(boldFont);
 
256
 
 
257
        italicAct = new QAction(tr(&quot;&amp;Italic&quot;), this);
 
258
        italicAct-&gt;setCheckable(true);
 
259
        italicAct-&gt;setShortcut(tr(&quot;Ctrl+I&quot;));
 
260
        italicAct-&gt;setStatusTip(tr(&quot;Make the text italic&quot;));
 
261
        connect(italicAct, SIGNAL(triggered()), this, SLOT(italic()));
 
262
 
 
263
        QFont italicFont = italicAct-&gt;font();
 
264
        italicFont.setItalic(true);
 
265
        italicAct-&gt;setFont(italicFont);
 
266
 
 
267
        leftAlignAct = new QAction(tr(&quot;&amp;Left Align&quot;), this);
 
268
        leftAlignAct-&gt;setCheckable(true);
 
269
        leftAlignAct-&gt;setShortcut(tr(&quot;Ctrl+L&quot;));
 
270
        leftAlignAct-&gt;setStatusTip(tr(&quot;Left align the selected text&quot;));
 
271
        connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign()));
 
272
 
 
273
        rightAlignAct = new QAction(tr(&quot;&amp;Right Align&quot;), this);
 
274
        rightAlignAct-&gt;setCheckable(true);
 
275
        rightAlignAct-&gt;setShortcut(tr(&quot;Ctrl+R&quot;));
 
276
        rightAlignAct-&gt;setStatusTip(tr(&quot;Right align the selected text&quot;));
 
277
        connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign()));
 
278
 
 
279
        justifyAct = new QAction(tr(&quot;&amp;Justify&quot;), this);
 
280
        justifyAct-&gt;setCheckable(true);
 
281
        justifyAct-&gt;setShortcut(tr(&quot;Ctrl+J&quot;));
 
282
        justifyAct-&gt;setStatusTip(tr(&quot;Justify the selected text&quot;));
 
283
        connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify()));
 
284
 
 
285
        centerAct = new QAction(tr(&quot;&amp;Center&quot;), this);
 
286
        centerAct-&gt;setCheckable(true);
 
287
        centerAct-&gt;setShortcut(tr(&quot;Ctrl+E&quot;));
 
288
        centerAct-&gt;setStatusTip(tr(&quot;Center the selected text&quot;));
 
289
        connect(centerAct, SIGNAL(triggered()), this, SLOT(center()));
 
290
 
 
291
        alignmentGroup = new QActionGroup(this);
 
292
        alignmentGroup-&gt;addAction(leftAlignAct);
 
293
        alignmentGroup-&gt;addAction(rightAlignAct);
 
294
        alignmentGroup-&gt;addAction(justifyAct);
 
295
        alignmentGroup-&gt;addAction(centerAct);
 
296
        leftAlignAct-&gt;setChecked(true);
 
297
 
 
298
        setLineSpacingAct = new QAction(tr(&quot;Set &amp;Line Spacing...&quot;), this);
 
299
        setLineSpacingAct-&gt;setStatusTip(tr(&quot;Change the gap between the lines of a &quot;
 
300
                                           &quot;paragraph&quot;));
 
301
        connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing()));
 
302
 
 
303
        setParagraphSpacingAct = new QAction(tr(&quot;Set &amp;Paragraph Spacing...&quot;), this);
 
304
        setLineSpacingAct-&gt;setStatusTip(tr(&quot;Change the gap between paragraphs&quot;));
 
305
        connect(setParagraphSpacingAct, SIGNAL(triggered()),
 
306
                this, SLOT(setParagraphSpacing()));
 
307
 
 
308
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
309
        aboutAct-&gt;setStatusTip(tr(&quot;Show the application's About box&quot;));
 
310
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
311
 
 
312
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
313
        aboutQtAct-&gt;setStatusTip(tr(&quot;Show the Qt library's About box&quot;));
 
314
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
315
        connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt()));
 
316
    }
 
317
 
 
318
    void MainWindow::createMenus()
 
319
    {
 
320
        fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
321
        fileMenu-&gt;addAction(newAct);
 
322
        fileMenu-&gt;addAction(openAct);
 
323
        fileMenu-&gt;addAction(saveAct);
 
324
        fileMenu-&gt;addAction(printAct);
 
325
        fileMenu-&gt;addSeparator();
 
326
        fileMenu-&gt;addAction(exitAct);
 
327
 
 
328
        editMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Edit&quot;));
 
329
        editMenu-&gt;addAction(undoAct);
 
330
        editMenu-&gt;addAction(redoAct);
 
331
        editMenu-&gt;addSeparator();
 
332
        editMenu-&gt;addAction(cutAct);
 
333
        editMenu-&gt;addAction(copyAct);
 
334
        editMenu-&gt;addAction(pasteAct);
 
335
        editMenu-&gt;addSeparator();
 
336
 
 
337
        formatMenu = editMenu-&gt;addMenu(tr(&quot;&amp;Format&quot;));
 
338
        formatMenu-&gt;addAction(boldAct);
 
339
        formatMenu-&gt;addAction(italicAct);
 
340
        formatMenu-&gt;addSeparator();
 
341
        formatMenu-&gt;addAction(leftAlignAct);
 
342
        formatMenu-&gt;addAction(rightAlignAct);
 
343
        formatMenu-&gt;addAction(justifyAct);
 
344
        formatMenu-&gt;addAction(centerAct);
 
345
        formatMenu-&gt;addSeparator();
 
346
        formatMenu-&gt;addAction(setLineSpacingAct);
 
347
        formatMenu-&gt;addAction(setParagraphSpacingAct);
 
348
 
 
349
        helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
 
350
        helpMenu-&gt;addAction(aboutAct);
 
351
        helpMenu-&gt;addAction(aboutQtAct);
 
352
    }</pre>
 
353
<p /><address><hr /><div align="center">
 
354
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
355
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
356
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
357
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
358
</tr></table></div></address></body>
 
359
</html>