且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Qt之文本编辑器(一)

更新时间:2022-02-20 03:31:04

今天开始呢,我们就开始用Qt做两个比较实用的东西,这一篇我们主要探究下文本编辑器的实现。

     首先我们来看下我们的大致框架:
 

  1. class MainWindow : public QMainWindow 
  2.     Q_OBJECT 
  3. public
  4.    MainWindow(); 
  5. protected
  6.     void closeEvent(QCloseEvent *event); 
对于所有定义的信号和槽的类,在类定义开始处的O_OBJECT宏都是必需的。
 

  1. private slots: 
  2.     void newFile(); 
  3.     void open(); 
  4.     bool save(); 
  5.     bool saveAs(); 
  6.     void about(); 
  7.     void documentWasModified(); 
私有槽中包含了创建新文件、打开文件、保存文件以及about。当然了我们还有一个在程序中最重要的函数documentWasModified(),实现的共ing功能是判断是否文件被修改。
 

  1. private
  2.     void createActions(); 
  3.     void createMenus(); 
  4.     void createToolBars(); 
  5.     void createStatusBar(); 
  6.     void readSettings(); 
  7.     void writeSettings(); 
  8.     bool maybeSave(); 
  9.     void loadFile(const QString &fileName); 
  10.     bool saveFile(const QString &fileName); 
  11.     void setCurrentFile(const QString &fileName); 
  12.     QString strippedName(const QString &fullFileName); 
  13.  
  14.  
  15.     QTextEdit *textEdit; 
  16.     QString curFile; 
  17.  
  18.     QMenu *fileMenu; 
  19.     QMenu *editMenu; 
  20.     QMenu *formMenu; 
  21.     QMenu *helpMenu; 
  22.  
  23.     QToolBar *fileToolBar; 
  24.     QToolBar *editToolBar; 
  25.  
  26.     QAction *newAct; 
  27.     QAction *openAct; 
  28.     QAction *saveAct; 
  29.     QAction *saveAsAct; 
  30.     QAction *exitAct; 
  31.     QAction *automaticAct; 
  32.     QAction *typefaceAct; 
  33.     QAction *cutAct; 
  34.     QAction *copyAct; 
  35.     QAction *pasteAct; 
  36.     QAction *aboutAct; 
  37.     QAction *aboutQtAct; 
  38. }; 
在这里面定义了在程序中用到的所有类的实例,createActions();createMenus();    createToolBars();createStatusBar();用于创建用户接口。readSetting()用于恢复用户以前的设置。下面我们就来一一看看具体实现:
 

  1. MainWindow::MainWindow() 
  2.     textEdit = new QTextEdit; 
  3.     setCentralWidget(textEdit); 
  4.     createActions(); 
  5.     createMenus(); 
  6.     createToolBars(); 
  7.     createStatusBar(); 
  8.     readSettings(); 
  9.  
  10.     connect(textEdit->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified)); 
  11.     setCurrentFile(""); 
我们的构造函数首先创建一个文本框实例,而后设置中心窗体,再者调用用户接口即可完成。
 

  1. void MainWindow::closeEvent(QCloseEvent *event) 
  2.     if(maybeSave()) 
  3.     { 
  4.         writeSettings(); 
  5.         event->accept(); 
  6.     } 
  7.     else 
  8.     { 
  9.         event->ignore(); 
  10.     } 
closeEvent()函数实现功能:在用户尝试退出时警告用户关于未保存的修改信息。
 

  1. void MainWindow::newFile() 
  2.     if(maybeSave()) 
  3.     { 
  4.         textEdit->clear(); 
  5.         setCurrentFile(""); 
  6.     } 
newFile函数实现功能:首先判断当前文件是否已保存,如果未保存先保存而后创建新文件。
 

  1. void MainWindow::open() 
  2.     if(maybeSave()) 
  3.     { 
  4.         QString fileName = QFileDialog::getOpenFileName(this); 
  5.         if(!fileName.isEmpty()) 
  6.             loadFile(fileName); 
  7.     } 
open()函数实现功能:首先判断当前文件是否已保存,如果未保存则先保存,而后通过QFileDialog的静态函数getOpenFileName()获取文件目录,打开。
 

  1. bool MainWindow::save() 
  2.     if(curFile.isEmpty()) 
  3.     { 
  4.         return saveAs(); 
  5.     } 
  6.     else 
  7.     { 
  8.         return saveFile(curFile); 
  9.     } 
  10. bool MainWindow::saveAs() 
  11.     QString fileName = QFileDialog::getSaveFileName(this); 
  12.     if(fileName.isEmpty()) 
  13.         return false
  14.     return saveFile(fileName); 
save() slot在用户点击File|Save菜单项时被调用。如果用户还没为文件提供一个名字,则调用saveAs(),否则调用saveFile()来保存文件。
 

  1. void MainWindow::about() 
  2.     QMessageBox::about(this, tr("About Application"), 
  3.                        tr("The <b>Application</b> example created by <b>Yzs</b>    ")); 
about()函数对话框通过QMessageBox::about()静态函数实现
 
 

  1. void MainWindow::documentWasModified() 
  2.     setWindowModified(textEdit->document()->isModified()); 
documentWasModified() slot在QTextEdit中的文本被改变时被调用。我们调用QWidget::setWindowModified()来是标题栏显示文件已被修改(显示个*号)。
 
 

  1. void MainWindow::createActions() 
  2.     newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this); 
  3.     newAct->setShortcut(tr("Ctrl+N")); 
  4.     newAct->setStatusTip(tr("Create a new file")); 
  5.     connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); 
  6.  
  7.     openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); 
  8.     openAct->setShortcut(tr("Ctrl+O")); 
  9.     openAct->setStatusTip(tr("Open an existing file")); 
  10.     connect(openAct, SIGNAL(triggered()), this, SLOT(open())); 
  11.  
  12.     saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this); 
  13.     saveAct->setShortcut(tr("Ctrl+S")); 
  14.     saveAct->setStatusTip(tr("Save a file")); 
  15.     connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); 
  16.  
  17.     saveAsAct = new QAction(tr("Save &As..."), this); 
  18.     saveAsAct->setStatusTip(tr("Save the file under a new name")); 
  19.     connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); 
  20.  
  21.     exitAct = new QAction(tr("E&xit"), this); 
  22.     exitAct->setShortcut(tr("Ctrl+Q")); 
  23.     exitAct->setStatusTip(tr("Exit the application")); 
  24.     connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); 
  25.  
  26.     automaticAct = new QAction(tr("&Automatic"), this); 
  27.     automaticAct->setChecked(false); 
  28.     automaticAct->setStatusTip(tr("Automatic the file")); 
  29.     connect(automaticAct, SIGNAL(triggered()), this, SLOT(automatic())); 
  30.  
  31.     typefaceAct = new QAction(tr("&Typeface"), this); 
  32.     typefaceAct->setStatusTip(tr("typefaceAct")); 
  33.     connect(typefaceAct, SIGNAL(triggered()), this, SLOT(typeface())); 
  34.  
  35.     cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this); 
  36.     cutAct->setShortcut(tr("Ctrl+X")); 
  37.     cutAct->setStatusTip(tr("Cut the current selection's contents to the clipboard")); 
  38.     connect(cutAct, SIGNAL(triggered()), this, SLOT(cut())); 
  39.  
  40.     copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this); 
  41.     copyAct->setShortcut(tr("Ctrl+C")); 
  42.     copyAct->setStatusTip(tr("Copy the current selection's contents to clipboard")); 
  43.     connect(copyAct, SIGNAL(triggered()), this, SLOT(copy())); 
  44.  
  45.     pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this); 
  46.     pasteAct->setShortcut(tr("Ctrl+V")); 
  47.     pasteAct->setStatusTip(tr("Paste the current selection's contents to clipboard")); 
  48.     connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste())); 
  49.  
  50.     aboutAct = new QAction(tr("&About"), this); 
  51.     aboutAct->setStatusTip(tr("Show the application's About box")); 
  52.     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); 
  53.  
  54.     aboutQtAct = new QAction(tr("About &Qt"), this); 
  55.     aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); 
  56.     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); 
  57.     cutAct->setEnabled(false); 
  58.     copyAct->setEnabled(false); 
  59.     connect(textEdit, SIGNAL(copyAvailable(bool)), 
  60.             cutAct, SLOT(setEnabled(bool))); 
  61.     connect(textEdit, SIGNAL(copyAvailable(bool)), 
  62.             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,如需转载请自行联系原作者