class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
protected:
void closeEvent(QCloseEvent *event);
private slots:
void newFile();
void open();
bool save();
bool saveAs();
void about();
void documentWasModified();
private:
void createActions();
void createMenus();
void createToolBars();
void createStatusBar();
void readSettings();
void writeSettings();
bool maybeSave();
void loadFile(const QString &fileName);
bool saveFile(const QString &fileName);
void setCurrentFile(const QString &fileName);
QString strippedName(const QString &fullFileName);
QTextEdit *textEdit;
QString curFile;
// 選單、工具列、動作指標...
};
Q_OBJECT 宏為訊號槽機制必要。
MainWindow::MainWindow() {
textEdit = new QTextEdit;
setCentralWidget(textEdit);
createActions();
createMenus();
createToolBars();
createStatusBar();
readSettings();
connect(textEdit->document(), SIGNAL(contentsChanged()),
this, SLOT(documentWasModified()));
setCurrentFile("");
}
void MainWindow::closeEvent(QCloseEvent *event) {
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
void MainWindow::newFile() {
if (maybeSave()) {
textEdit->clear();
setCurrentFile("");
}
}
void MainWindow::open() {
if (maybeSave()) {
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty()) loadFile(fileName);
}
}
bool MainWindow::save() {
if (curFile.isEmpty()) return saveAs();
else return saveFile(curFile);
}
bool MainWindow::saveAs() {
QString fileName = QFileDialog::getSaveFileName(this);
if (fileName.isEmpty()) return false;
return saveFile(fileName);
}
void MainWindow::about() {
QMessageBox::about(this, tr("About Application"),
tr("The <b>Application</b> example created by <b>Yzs</b>"));
}
void MainWindow::documentWasModified() {
setWindowModified(textEdit->document()->isModified());
}
void MainWindow::createActions() {
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAct->setShortcut(tr("Ctrl+S"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("Save &As..."), this);
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
cutAct->setShortcut(tr("Ctrl+X"));
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
copyAct->setShortcut(tr("Ctrl+C"));
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
pasteAct->setShortcut(tr("Ctrl+V"));
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
// 剪貼/複製需有選取文字才啟用
cutAct->setEnabled(false);
copyAct->setEnabled(false);
connect(textEdit, SIGNAL(copyAvailable(bool)), cutAct, SLOT(setEnabled(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), copyAct, SLOT(setEnabled(bool)));
}
QAction 封裝使用者操作,可同時加入選單與工具列。triggered() 訊號連接對應 slot 實作功能。編輯動作預設停用,透過 copyAvailable(bool) 訊號動態啟用。