Qt的文本编辑器(上)
当前位置:以往代写 > C/C++ 教程 >Qt的文本编辑器(上)
2019-06-13

Qt的文本编辑器(上)

Qt的文本编辑器(上)

副标题#e#

本日开始呢,我们就开始用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;     
};


#p#副标题#e#

在这内里界说了在措施顶用到的所有类的实例,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);     
}

#p#副标题#e#

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)));     
}

#p#分页标题#e#

QAction是一个代表用户行为的工具,譬喻,生存文件或弹出对话框。一个action可以被放入QMenu或QToolBar中,也可以被放入其它重载了QWidget::actionEvent()的widget中。一个action有一个用于描写它浸染的文本,当用户触发这个action时,它发出triggered()信号。我们将这个信号毗连到一个slot上以实现真正的成果。Edit|Cut和Edit|Copy action必需在QTextEdit包括已选择的文本时才有效。在默认环境下我们将它们禁用并将QTextEdit::copyAvailable()信号毗连到QAction::setEnabled() slot上,以确保在文本编辑器中没有选择文本时着两个行为无效。

本文出自 “驿落薄暮” 博客,请务必保存此出处http://yiluohuanghun.blog.51cto.com/3407300/959827

    关键字:

在线提交作业