Qt:多文檔(MDI)文檔處理軟件思路01

文檔處理軟件是咱們平常生活中最爲經常使用的軟件之一。在此以將記事本爲例子,實現的基本思路描述。app

一:基本外觀功能。工具

1)有菜單欄和按鈕,根據不一樣的實現功能,將按鈕添加到菜單中,而且添加工具欄。ui

2)主窗口顯示(在此不一樣於記事本,爲多文本窗口)。this

在Qt中按鈕的顯示以QAction來替代,菜單和工具欄爲QMenu和QToolBar,部分代碼以下:spa

    //in file menu.
    QAction* pActionNew;
    QAction* pActionOpen;
    QAction* pActionSave;
    QAction* pActionSaveAs;
    QAction* pActionExportPdf;
    QAction* pActionExit;

    QMenu* pMenuFile;
    QMenu* pMenuWindow;

由於主窗口是多文檔顯示,因此要在主窗口添加QMdiArea,以保存多文檔。code

QMdiArea* pMdiArea;


由於每一個QAction實例都需對應一個相應的方法,故聲明new、open、save、saveas、print、exit等file菜單需調用的方法。orm

如下爲完整的mainwindow.h文件:ip

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QMdiArea;
class QAction;
class TextEditor;
class QMenu;
class QActionGroup;
class TextEditor;
class QCloseEvent;
class QDragEnterEvent;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    virtual void closeEvent(QCloseEvent* cle);

private:
    void setupFileMenuActions();

    void setupMenusAndToolbar();

    /**
      @brief add TextEditor instance to MDIArea.
      @param editor TextEditor instance.
    */
    void addEditor(TextEditor* editor);

    /**
      @brief new a file in MDIArea.
    */
    void newFile();

    /**
      @brief open a exist file.
    */
    void openFile();

    /**
      @brief save file in defalut configuration.
    */
    bool save();

    /**
      @brief save file by user option.
    */
    bool saveAs();

    /**
      @brief export file in pdf format.
    */
    void exportInPdfFormat();

    /**
      @brief find the active TextEditor and return it.
      @return active TextEditor instance in MDIArea.
    */
    TextEditor* activeEditor();

private:
    Ui::MainWindow *ui;

    //in file menu.
    QAction* pActionNew;
    QAction* pActionOpen;
    QAction* pActionSave;
    QAction* pActionSaveAs;
    QAction* pActionExportPdf;
    QAction* pActionExit;

    QMenu* pMenuFile;
    QMenu* pMenuWindow;

    QMdiArea* pMdiArea;
    QActionGroup* pWindowMenuGroup;

    static QString mResWinPath;
};

#endif // MAINWINDOW_H

由於此程序爲多窗口(Mdi)程序,因此針對程序中不一樣的文本框有不一樣的配置,例如文檔保存狀況,是否爲active,不一樣的文件名及路徑,因此將具體對文檔的操做實如今文檔類中(QTextEdit的子類),mainwindow只是對其的具體調用。
ci

如下是mainwindow.cpp的具體實現:文檔

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMdiArea>
#include <QAction>
#include <QMenu>
#include <QMdiSubWindow>
#include <QActionGroup>
#include <QCloseEvent>
#include <QDragEnterEvent>

#include "texteditor.h"

QString MainWindow::mResWinPath = ":/images/win/";

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    pMdiArea(new QMdiArea(this)),
    pWindowMenuGroup(new QActionGroup(this))
{
    ui->setupUi(this);
    setCentralWidget(pMdiArea);

    setupFileMenuActions();

    setupMenusAndToolbar();

    setAttribute(Qt::WA_DeleteOnClose);
    setAcceptDrops(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent* cle)
{
    pMdiArea->closeAllSubWindows();

    if ( !pMdiArea->subWindowList().isEmpty()) {
        cle->ignore();
    } else {
        cle->accept();
    }
}

void MainWindow::setupFileMenuActions()
{
    pActionNew = new QAction(QIcon(MainWindow::mResWinPath + "filenew.png"),
                             tr("&New"),
                             this);
    pActionNew->setShortcut(QKeySequence::New);
    pActionNew->setPriority(QAction::LowPriority);
    pActionNew->setStatusTip(tr("Create a new file"));
    connect(pActionNew, &QAction::triggered, this, &MainWindow::newFile);

    pActionOpen = new QAction(QIcon(MainWindow::mResWinPath + "fileopen.png"),
                              tr("&Open"),
                              this);
    pActionOpen->setShortcut(QKeySequence::Open);
    pActionOpen->setPriority(QAction::LowPriority);
    pActionOpen->setStatusTip(tr("Open a file"));
    connect(pActionOpen, &QAction::triggered, this, &MainWindow::openFile);

    pActionSave = new QAction(QIcon(MainWindow::mResWinPath + "filesave.png"),
                              tr("&Save"),
                              this);
    pActionSave->setShortcut(QKeySequence::Save);
    pActionSave->setStatusTip(tr("save this file"));

    pActionSaveAs = new QAction(tr("Save As"), this);
    pActionSaveAs->setShortcut(QKeySequence::SaveAs);
    pActionSaveAs->setStatusTip(tr("Save this file as..."));
    connect(pActionSaveAs, &QAction::triggered, this, &MainWindow::saveAs);

    pActionExportPdf = new QAction(QIcon(MainWindow::mResWinPath + "exportpdf.png"),
                                   tr("Export Pdf"),
                                   this);
    pActionExportPdf->setStatusTip(tr("export file in pdf format"));
    connect(pActionExportPdf, &QAction::triggered, this, &MainWindow::exportInPdfFormat);

    pActionExit = new QAction(tr("E&xit"), this);
    pActionExit->setShortcut(QKeySequence::Quit);
    pActionExit->setStatusTip(tr("Exit this application"));
    connect(pActionExit, &QAction::triggered, qApp, &QApplication::closeAllWindows);
}

void MainWindow::setupMenusAndToolbar()
{
    pMenuFile = ui->menuBar->addMenu(tr("&File"));
    pMenuFile->addAction(pActionNew);
    pMenuFile->addAction(pActionOpen);
    pMenuFile->addAction(pActionSaveAs);
    pMenuFile->addAction(pActionExportPdf);
    pMenuFile->addAction(pActionExit);

    QToolBar* tb = addToolBar(tr("FileToolBar"));
    tb->addAction(pActionNew);
    tb->addAction(pActionOpen);
    tb->addAction(pActionSave);
    tb->addAction(pActionExportPdf);

    pMenuWindow = ui->menuBar->addMenu(tr("&Window"));
}

void MainWindow::addEditor(TextEditor* editor)
{
    //Add action to windowMenuGroup for RadioGroup.
    pMenuWindow->addAction(editor->windowMenuAction());
    pWindowMenuGroup->addAction(editor->windowMenuAction());

    QMdiSubWindow* subWindow = pMdiArea->addSubWindow(editor);
    subWindow->show();
    subWindow->setFocus();
}

void MainWindow::newFile()
{
    //Create a new editor and add it to MDIArea.
    TextEditor* editor(new TextEditor(this));
    editor->newFile();
    addEditor(editor);
}

void MainWindow::openFile()
{
    //Call TextEditor::open method for user to choose file.
    TextEditor* editor = TextEditor::open(this);

    //true If choose file successfully,add the file to MDIArea.
    if ( editor) {
        addEditor(editor);
    }
}

bool MainWindow::save()
{
    TextEditor* editor = activeEditor();

    if ( editor) {
        return editor->save();
    } else {
        return false;
    }
}

bool MainWindow::saveAs()
{
    TextEditor* editor = activeEditor();

    if ( editor) {
        return editor->saveAs();
    }

    return false;
}

void MainWindow::exportInPdfFormat()
{
    TextEditor* editor = activeEditor();

    if ( editor) {
        editor->fileExportToPDF();
    }
}

TextEditor* MainWindow::activeEditor()
{
    QMdiSubWindow* subWindow = pMdiArea->activeSubWindow();

    if ( subWindow) {
        return qobject_cast<TextEditor*>(subWindow->widget());
    } else {
        return 0;
    }
}
相關文章
相關標籤/搜索