4
#include "%{APPNAMELC}.h"
11
%{APPNAME}::%{APPNAME}()
13
textEdit = new QTextEdit;
14
setCentralWidget(textEdit);
23
connect(textEdit->document(), SIGNAL(contentsChanged()),
24
this, SLOT(documentWasModified()));
29
void %{APPNAME}::closeEvent(QCloseEvent *event)
39
void %{APPNAME}::newFile()
47
void %{APPNAME}::open()
50
QString fileName = QFileDialog::getOpenFileName(this);
51
if (!fileName.isEmpty())
56
bool %{APPNAME}::save()
58
if (curFile.isEmpty()) {
61
return saveFile(curFile);
65
bool %{APPNAME}::saveAs()
67
QString fileName = QFileDialog::getSaveFileName(this);
68
if (fileName.isEmpty())
71
return saveFile(fileName);
74
void %{APPNAME}::about()
76
QMessageBox::about(this, tr("About Application"),
77
tr("The <b>Application</b> example demonstrates how to "
78
"write modern GUI applications using Qt, with a menu bar, "
79
"toolbars, and a status bar."));
82
void %{APPNAME}::documentWasModified()
84
setWindowModified(true);
87
void %{APPNAME}::createActions()
89
newAct = new QAction(QIcon(":/filenew.xpm"), tr("&New"), this);
90
newAct->setShortcut(tr("Ctrl+N"));
91
newAct->setStatusTip(tr("Create a new file"));
92
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
94
openAct = new QAction(QIcon(":/fileopen.xpm"), tr("&Open..."), this);
95
openAct->setShortcut(tr("Ctrl+O"));
96
openAct->setStatusTip(tr("Open an existing file"));
97
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
99
saveAct = new QAction(QIcon(":/filesave.xpm"), tr("&Save"), this);
100
saveAct->setShortcut(tr("Ctrl+S"));
101
saveAct->setStatusTip(tr("Save the document to disk"));
102
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
104
saveAsAct = new QAction(tr("Save &As..."), this);
105
saveAsAct->setStatusTip(tr("Save the document under a new name"));
106
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
108
exitAct = new QAction(tr("E&xit"), this);
109
exitAct->setShortcut(tr("Ctrl+Q"));
110
exitAct->setStatusTip(tr("Exit the application"));
111
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
113
cutAct = new QAction(QIcon(":/editcut.xpm"), tr("Cu&t"), this);
114
cutAct->setShortcut(tr("Ctrl+X"));
115
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
117
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
119
copyAct = new QAction(QIcon(":/editcopy.xpm"), tr("&Copy"), this);
120
copyAct->setShortcut(tr("Ctrl+C"));
121
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
123
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
125
pasteAct = new QAction(QIcon(":/editpaste.xpm"), tr("&Paste"), this);
126
pasteAct->setShortcut(tr("Ctrl+V"));
127
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
129
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
131
aboutAct = new QAction(tr("&About"), this);
132
aboutAct->setStatusTip(tr("Show the application's About box"));
133
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
135
aboutQtAct = new QAction(tr("About &Qt"), this);
136
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
137
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
139
cutAct->setEnabled(false);
140
copyAct->setEnabled(false);
141
connect(textEdit, SIGNAL(copyAvailable(bool)),
142
cutAct, SLOT(setEnabled(bool)));
143
connect(textEdit, SIGNAL(copyAvailable(bool)),
144
copyAct, SLOT(setEnabled(bool)));
147
void %{APPNAME}::createMenus()
149
fileMenu = menuBar()->addMenu(tr("&File"));
150
fileMenu->addAction(newAct);
151
fileMenu->addAction(openAct);
152
fileMenu->addAction(saveAct);
153
fileMenu->addAction(saveAsAct);
154
fileMenu->addSeparator();
155
fileMenu->addAction(exitAct);
157
editMenu = menuBar()->addMenu(tr("&Edit"));
158
editMenu->addAction(cutAct);
159
editMenu->addAction(copyAct);
160
editMenu->addAction(pasteAct);
162
menuBar()->addSeparator();
164
helpMenu = menuBar()->addMenu(tr("&Help"));
165
helpMenu->addAction(aboutAct);
166
helpMenu->addAction(aboutQtAct);
169
void %{APPNAME}::createToolBars()
171
fileToolBar = addToolBar(tr("File"));
172
fileToolBar->addAction(newAct);
173
fileToolBar->addAction(openAct);
174
fileToolBar->addAction(saveAct);
176
editToolBar = addToolBar(tr("Edit"));
177
editToolBar->addAction(cutAct);
178
editToolBar->addAction(copyAct);
179
editToolBar->addAction(pasteAct);
182
void %{APPNAME}::createStatusBar()
184
statusBar()->showMessage(tr("Ready"));
187
void %{APPNAME}::readSettings()
189
QSettings settings("Trolltech", "Application Example");
190
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
191
QSize size = settings.value("size", QSize(400, 400)).toSize();
196
void %{APPNAME}::writeSettings()
198
QSettings settings("Trolltech", "Application Example");
199
settings.setValue("pos", pos());
200
settings.setValue("size", size());
203
bool %{APPNAME}::maybeSave()
205
if (textEdit->document()->isModified()) {
206
int ret = QMessageBox::warning(this, tr("Application"),
207
tr("The document has been modified.\n"
208
"Do you want to save your changes?"),
209
QMessageBox::Yes | QMessageBox::Default,
211
QMessageBox::Cancel | QMessageBox::Escape);
212
if (ret == QMessageBox::Yes)
214
else if (ret == QMessageBox::Cancel)
220
void %{APPNAME}::loadFile(const QString &fileName)
222
QFile file(fileName);
223
if (!file.open(QFile::ReadOnly | QFile::Text)) {
224
QMessageBox::warning(this, tr("Application"),
225
tr("Cannot read file %1:\n%2.")
227
.arg(file.errorString()));
231
QTextStream in(&file);
232
QApplication::setOverrideCursor(Qt::WaitCursor);
233
textEdit->setPlainText(in.readAll());
234
QApplication::restoreOverrideCursor();
236
setCurrentFile(fileName);
237
statusBar()->showMessage(tr("File loaded"), 2000);
240
bool %{APPNAME}::saveFile(const QString &fileName)
242
QFile file(fileName);
243
if (!file.open(QFile::WriteOnly | QFile::Text)) {
244
QMessageBox::warning(this, tr("Application"),
245
tr("Cannot write file %1:\n%2.")
247
.arg(file.errorString()));
251
QTextStream out(&file);
252
QApplication::setOverrideCursor(Qt::WaitCursor);
253
out << textEdit->toPlainText();
254
QApplication::restoreOverrideCursor();
256
setCurrentFile(fileName);
257
statusBar()->showMessage(tr("File saved"), 2000);
261
void %{APPNAME}::setCurrentFile(const QString &fileName)
264
textEdit->document()->setModified(false);
265
setWindowModified(false);
268
if (curFile.isEmpty())
269
shownName = "untitled.txt";
271
shownName = strippedName(curFile);
273
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Application")));
276
QString %{APPNAME}::strippedName(const QString &fullFileName)
278
return QFileInfo(fullFileName).fileName();
281
%{APPNAME}::~%{APPNAME}()