QT: Multi-document (MDI) document processing software Ideas 01

Source: Internet
Author: User

Document processing software is one of the most commonly used software in our daily life. In this take Notepad as an example, the implementation of the basic ideas described.

A: basic appearance function.

1) There are menu bar and buttons, according to different implementation functions, add buttons to the menu, and add Toolbars.

2) The main window is displayed (this differs from Notepad, which is a multi-text window).

The display of the buttons in Qt is replaced by qaction, the menus and toolbars are qmenu and Qtoolbar, and some of the code is as follows:

in File menu.    qaction* pactionnew;    qaction* Pactionopen;    qaction* Pactionsave;    qaction* Pactionsaveas;    qaction* pactionexportpdf;    qaction* Pactionexit;    qmenu* Pmenufile; qmenu* Pmenuwindow;

Because the main window is multi-document display, you add Qmdiarea to the main window to save multiple documents.

qmdiarea* Pmdiarea;


Because each qaction instance needs to correspond to a corresponding method, it declares the method to be called by the File menu such as new, open, save, SaveAs, print, exit, and so on.

The following is the complete Mainwindow.h file:

#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_objectpublic:     explicit mainwindow (qwidget *parent = 0);     ~mainwindow () ;p rotected:    virtual void closeevent (qcloseevent* cle);p rivate:     void setupfilemenuactions ();     void setupmenusandtoolbar ();     /**       @brief  add texteditor instance  to MDIArea.       @param  editor texteditor instance.     */   &nbsP;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 ();p rivate:    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

Because this program is a multi-window (MDI) program, so there are different configurations for different text boxes in the program, such as document preservation, whether it is active, different file names and paths, so the specific operation of the document is implemented in the document class (subclass of Qtextedit), MainWindow is just a specific call to it.

The following are the specific implementations of 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"),                             &NBSP;&NBSP;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"),                     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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"),                             &NBSP;&NBSP;&NBSP;TR ("&save"),                                this);     pactionsave->setshortcut (Qkeysequence::save);     Pactionsave->setstatustip (tr ("Save this file"));     pactionsaveas =  new qaction (tr ("Save as"),  this);     pactionsaveas->setshortcut ( Qkeysequence::saveas);   &nbsP; pactionsaveas->setstatustip (tr ("Save this file as ..."));     Connect (Pactionsaveas, &qaction::triggered, this, &mainwindow::saveas);     pactionexportpdf = new qaction (Qicon (mainwindow::mreswinpath +  " Exportpdf.png "),                                   &NBSP;&NBSP;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));    &nBsp;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;    }}


QT: Multi-document (MDI) document processing software Ideas 01

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.