今天开始呢,我们就开始用Qt做两个比较实用的东西,这一篇我们主要探究下文本编辑器的实现。
首先我们来看下我们的大致框架:
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow();
- protected:
- void closeEvent(QCloseEvent *event);
对于所有定义的信号和槽的类,在类定义开始处的O_OBJECT宏都是必需的。
- private slots:
- void newFile();
- void open();
- bool save();
- bool saveAs();
- void about();
- void documentWasModified();
私有槽中包含了创建新文件、打开文件、保存文件以及about。当然了我们还有一个在程序中最重要的函数documentWasModified(),实现的共ing功能是判断是否文件被修改。
- 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;
- };
在这里面定义了在程序中用到的所有类的实例,createActions();createMenus(); createToolBars();createStatusBar();用于创建用户接口。readSetting()用于恢复用户以前的设置。下面我们就来一一看看具体实现:
- 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();
- }
- }
closeEvent()函数实现功能:在用户尝试退出时警告用户关于未保存的修改信息。
- void MainWindow::newFile()
- {
- if(maybeSave())
- {
- textEdit->clear();
- setCurrentFile("");
- }
- }
newFile函数实现功能:首先判断当前文件是否已保存,如果未保存先保存而后创建新文件。
- void MainWindow::open()
- {
- if(maybeSave())
- {
- QString fileName = QFileDialog::getOpenFileName(this);
- if(!fileName.isEmpty())
- loadFile(fileName);
- }
- }
open()函数实现功能:首先判断当前文件是否已保存,如果未保存则先保存,而后通过QFileDialog的静态函数getOpenFileName()获取文件目录,打开。
- 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);
- }
save() slot在用户点击File|Save菜单项时被调用。如果用户还没为文件提供一个名字,则调用saveAs(),否则调用saveFile()来保存文件。
- void MainWindow::about()
- {
- QMessageBox::about(this, tr("About Application"),
- tr("The <b>Application</b> example created by <b>Yzs</b> "));
- }
about()函数对话框通过QMessageBox::about()静态函数实现
- void MainWindow::documentWasModified()
- {
- setWindowModified(textEdit->document()->isModified());
- }
documentWasModified() slot在QTextEdit中的文本被改变时被调用。我们调用QWidget::setWindowModified()来是标题栏显示文件已被修改(显示个*号)。
- 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)));
- }
QAction是一个代表用户行为的对象,例如,保存文件或弹出对话框。一个action可以被放入QMenu或QToolBar中,也可以被放入其它重载了QWidget::actionEvent()的widget中。一个action有一个用于描述它作用的文本,当用户触发这个action时,它发出triggered()信号。我们将这个信号连接到一个slot上以实现真正的功能。Edit|Cut和Edit|Copy action必须在QTextEdit包含已选择的文本时才有效。在默认情况下我们将它们禁用并将QTextEdit::copyAvailable()信号连接到QAction::setEnabled() slot上,以确保在文本编辑器中没有选择文本时着两个行为无效。
本文转自 驿落黄昏 51CTO博客,原文链接:http://blog.51cto.com/yiluohuanghun/959827,如需转载请自行联系原作者