1
<?xml version="1.0" encoding="iso-8859-1"?>
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">
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>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" 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>
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> /****************************************************************************
22
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
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.
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.
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.
40
** Contact info@trolltech.com if any conditions of this licensing are
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.
46
****************************************************************************/
48
#include <QtGui>
50
#include "mainwindow.h"
52
MainWindow::MainWindow()
54
QWidget *w = new QWidget;
57
QWidget *topFiller = new QWidget;
58
topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
60
infoLabel = new QLabel(tr("<i>Choose a menu option, or right-click to "
61
"invoke a context menu</i>"));
62
infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
63
infoLabel->setAlignment(Qt::AlignCenter);
65
QWidget *bottomFiller = new QWidget;
66
bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
68
QVBoxLayout *vbox = new QVBoxLayout;
69
vbox->setMargin(5);
70
vbox->addWidget(topFiller);
71
vbox->addWidget(infoLabel);
72
vbox->addWidget(bottomFiller);
73
w->setLayout(vbox);
78
statusBar()->showMessage(tr("A context menu is available by right-clicking"));
80
setWindowTitle(tr("Menus"));
81
setMinimumSize(160, 160);
85
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
88
menu.addAction(cutAct);
89
menu.addAction(copyAct);
90
menu.addAction(pasteAct);
91
menu.exec(event->globalPos());
94
void MainWindow::newFile()
96
infoLabel->setText(tr("Invoked <b>File|New</b>"));
99
void MainWindow::open()
101
infoLabel->setText(tr("Invoked <b>File|Open</b>"));
104
void MainWindow::save()
106
infoLabel->setText(tr("Invoked <b>File|Save</b>"));
109
void MainWindow::print()
111
infoLabel->setText(tr("Invoked <b>File|Print</b>"));
114
void MainWindow::undo()
116
infoLabel->setText(tr("Invoked <b>Edit|Undo</b>"));
119
void MainWindow::redo()
121
infoLabel->setText(tr("Invoked <b>Edit|Redo</b>"));
124
void MainWindow::cut()
126
infoLabel->setText(tr("Invoked <b>Edit|Cut</b>"));
129
void MainWindow::copy()
131
infoLabel->setText(tr("Invoked <b>Edit|Copy</b>"));
134
void MainWindow::paste()
136
infoLabel->setText(tr("Invoked <b>Edit|Paste</b>"));
139
void MainWindow::bold()
141
infoLabel->setText(tr("Invoked <b>Edit|Format|Bold</b>"));
144
void MainWindow::italic()
146
infoLabel->setText(tr("Invoked <b>Edit|Format|Italic</b>"));
149
void MainWindow::leftAlign()
151
infoLabel->setText(tr("Invoked <b>Edit|Format|Left Align</b>"));
154
void MainWindow::rightAlign()
156
infoLabel->setText(tr("Invoked <b>Edit|Format|Right Align</b>"));
159
void MainWindow::justify()
161
infoLabel->setText(tr("Invoked <b>Edit|Format|Justify</b>"));
164
void MainWindow::center()
166
infoLabel->setText(tr("Invoked <b>Edit|Format|Center</b>"));
169
void MainWindow::setLineSpacing()
171
infoLabel->setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>"));
174
void MainWindow::setParagraphSpacing()
176
infoLabel->setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>"));
179
void MainWindow::about()
181
infoLabel->setText(tr("Invoked <b>Help|About</b>"));
182
QMessageBox::about(this, tr("About Menu"),
183
tr("The <b>Menu</b> example shows how to create "
184
"menu-bar menus and context menus."));
187
void MainWindow::aboutQt()
189
infoLabel->setText(tr("Invoked <b>Help|About Qt</b>"));
192
void MainWindow::createActions()
194
newAct = new QAction(tr("&New"), this);
195
newAct->setShortcut(tr("Ctrl+N"));
196
newAct->setStatusTip(tr("Create a new file"));
197
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
199
openAct = new QAction(tr("&Open..."), this);
200
openAct->setShortcut(tr("Ctrl+O"));
201
openAct->setStatusTip(tr("Open an existing file"));
202
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
204
saveAct = new QAction(tr("&Save"), this);
205
saveAct->setShortcut(tr("Ctrl+S"));
206
saveAct->setStatusTip(tr("Save the document to disk"));
207
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
209
printAct = new QAction(tr("&Print..."), this);
210
printAct->setShortcut(tr("Ctrl+P"));
211
printAct->setStatusTip(tr("Print the document"));
212
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
214
exitAct = new QAction(tr("E&xit"), this);
215
exitAct->setShortcut(tr("Ctrl+Q"));
216
exitAct->setStatusTip(tr("Exit the application"));
217
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
219
undoAct = new QAction(tr("&Undo"), this);
220
undoAct->setShortcut(tr("Ctrl+Z"));
221
undoAct->setStatusTip(tr("Undo the last operation"));
222
connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
224
redoAct = new QAction(tr("&Redo"), this);
225
redoAct->setShortcut(tr("Ctrl+Y"));
226
redoAct->setStatusTip(tr("Redo the last operation"));
227
connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
229
cutAct = new QAction(tr("Cu&t"), this);
230
cutAct->setShortcut(tr("Ctrl+X"));
231
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
232
"clipboard"));
233
connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
235
copyAct = new QAction(tr("&Copy"), this);
236
copyAct->setShortcut(tr("Ctrl+C"));
237
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
238
"clipboard"));
239
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
241
pasteAct = new QAction(tr("&Paste"), this);
242
pasteAct->setShortcut(tr("Ctrl+V"));
243
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
244
"selection"));
245
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
247
boldAct = new QAction(tr("&Bold"), this);
248
boldAct->setCheckable(true);
249
boldAct->setShortcut(tr("Ctrl+B"));
250
boldAct->setStatusTip(tr("Make the text bold"));
251
connect(boldAct, SIGNAL(triggered()), this, SLOT(bold()));
253
QFont boldFont = boldAct->font();
254
boldFont.setBold(true);
255
boldAct->setFont(boldFont);
257
italicAct = new QAction(tr("&Italic"), this);
258
italicAct->setCheckable(true);
259
italicAct->setShortcut(tr("Ctrl+I"));
260
italicAct->setStatusTip(tr("Make the text italic"));
261
connect(italicAct, SIGNAL(triggered()), this, SLOT(italic()));
263
QFont italicFont = italicAct->font();
264
italicFont.setItalic(true);
265
italicAct->setFont(italicFont);
267
leftAlignAct = new QAction(tr("&Left Align"), this);
268
leftAlignAct->setCheckable(true);
269
leftAlignAct->setShortcut(tr("Ctrl+L"));
270
leftAlignAct->setStatusTip(tr("Left align the selected text"));
271
connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign()));
273
rightAlignAct = new QAction(tr("&Right Align"), this);
274
rightAlignAct->setCheckable(true);
275
rightAlignAct->setShortcut(tr("Ctrl+R"));
276
rightAlignAct->setStatusTip(tr("Right align the selected text"));
277
connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign()));
279
justifyAct = new QAction(tr("&Justify"), this);
280
justifyAct->setCheckable(true);
281
justifyAct->setShortcut(tr("Ctrl+J"));
282
justifyAct->setStatusTip(tr("Justify the selected text"));
283
connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify()));
285
centerAct = new QAction(tr("&Center"), this);
286
centerAct->setCheckable(true);
287
centerAct->setShortcut(tr("Ctrl+E"));
288
centerAct->setStatusTip(tr("Center the selected text"));
289
connect(centerAct, SIGNAL(triggered()), this, SLOT(center()));
291
alignmentGroup = new QActionGroup(this);
292
alignmentGroup->addAction(leftAlignAct);
293
alignmentGroup->addAction(rightAlignAct);
294
alignmentGroup->addAction(justifyAct);
295
alignmentGroup->addAction(centerAct);
296
leftAlignAct->setChecked(true);
298
setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this);
299
setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a "
300
"paragraph"));
301
connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing()));
303
setParagraphSpacingAct = new QAction(tr("Set &Paragraph Spacing..."), this);
304
setLineSpacingAct->setStatusTip(tr("Change the gap between paragraphs"));
305
connect(setParagraphSpacingAct, SIGNAL(triggered()),
306
this, SLOT(setParagraphSpacing()));
308
aboutAct = new QAction(tr("&About"), this);
309
aboutAct->setStatusTip(tr("Show the application's About box"));
310
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
312
aboutQtAct = new QAction(tr("About &Qt"), this);
313
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
314
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
315
connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt()));
318
void MainWindow::createMenus()
320
fileMenu = menuBar()->addMenu(tr("&File"));
321
fileMenu->addAction(newAct);
322
fileMenu->addAction(openAct);
323
fileMenu->addAction(saveAct);
324
fileMenu->addAction(printAct);
325
fileMenu->addSeparator();
326
fileMenu->addAction(exitAct);
328
editMenu = menuBar()->addMenu(tr("&Edit"));
329
editMenu->addAction(undoAct);
330
editMenu->addAction(redoAct);
331
editMenu->addSeparator();
332
editMenu->addAction(cutAct);
333
editMenu->addAction(copyAct);
334
editMenu->addAction(pasteAct);
335
editMenu->addSeparator();
337
formatMenu = editMenu->addMenu(tr("&Format"));
338
formatMenu->addAction(boldAct);
339
formatMenu->addAction(italicAct);
340
formatMenu->addSeparator();
341
formatMenu->addAction(leftAlignAct);
342
formatMenu->addAction(rightAlignAct);
343
formatMenu->addAction(justifyAct);
344
formatMenu->addAction(centerAct);
345
formatMenu->addSeparator();
346
formatMenu->addAction(setLineSpacingAct);
347
formatMenu->addAction(setParagraphSpacingAct);
349
helpMenu = menuBar()->addMenu(tr("&Help"));
350
helpMenu->addAction(aboutAct);
351
helpMenu->addAction(aboutQtAct);
353
<p /><address><hr /><div align="center">
354
<table width="100%" cellspacing="0" border="0"><tr class="address">
355
<td width="30%">Copyright © 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>