今天开始呢,我们就开始用Qt做两个比较实用的东西,这一篇我们主要探究下文本编辑器的实现。
- 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;
- QMenu *fileMenu;
- QMenu *editMenu;
- QMenu *formMenu;
- QMenu *helpMenu;
- QToolBar *fileToolBar;
- QToolBar *editToolBar;
- QAction *newAct;
- QAction *openAct;
- QAction *saveAct;
- QAction *saveAsAct;
- QAction *exitAct;
- QAction *automaticAct;
- QAction *typefaceAct;
- QAction *cutAct;
- QAction *copyAct;
- QAction *pasteAct;
- QAction *aboutAct;
- QAction *aboutQtAct;
- };
- 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"));
- openAct->setStatusTip(tr("Open an existing file"));
- connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
- saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
- saveAct->setShortcut(tr("Ctrl+S"));
- saveAct->setStatusTip(tr("Save a file"));
- connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
- saveAsAct = new QAction(tr("Save &As..."), this);
- saveAsAct->setStatusTip(tr("Save the file under a new name"));
- connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
- exitAct = new QAction(tr("E&xit"), this);
- exitAct->setShortcut(tr("Ctrl+Q"));
- exitAct->setStatusTip(tr("Exit the application"));
- connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
- automaticAct = new QAction(tr("&Automatic"), this);
- automaticAct->setChecked(false);
- automaticAct->setStatusTip(tr("Automatic the file"));
- connect(automaticAct, SIGNAL(triggered()), this, SLOT(automatic()));
- typefaceAct = new QAction(tr("&Typeface"), this);
- typefaceAct->setStatusTip(tr("typefaceAct"));
- connect(typefaceAct, SIGNAL(triggered()), this, SLOT(typeface()));
- cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
- cutAct->setShortcut(tr("Ctrl+X"));
- cutAct->setStatusTip(tr("Cut the current selection's contents to the clipboard"));
- connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
- copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
- copyAct->setShortcut(tr("Ctrl+C"));
- copyAct->setStatusTip(tr("Copy the current selection's contents to clipboard"));
- connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
- pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
- pasteAct->setShortcut(tr("Ctrl+V"));
- pasteAct->setStatusTip(tr("Paste the current selection's contents to clipboard"));
- connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
- aboutAct = new QAction(tr("&About"), this);
- aboutAct->setStatusTip(tr("Show the application's About box"));
- connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
- aboutQtAct = new QAction(tr("About &Qt"), this);
- aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
- 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)));
- }